Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by robinking · Jan 30, 2013 at 05:06 PM · c#conversiongenerics

How can I make a generic class convertible in type?

If I have a generic class:

 class Collection < G >  {
     public G[] stuff
 }

and I have two other classes, one of which can convert to the other (though more complex than this!)

 class Foo {
     public Foo( int i ) { myInt = i; }
     public int myInt;
 }
 class Bar {
     public Bar( float f ) { myFloat = f; }
     public float myFloat;
     public static implicit operator Foo( Bar bar ) {
         return new Foo( Math.Ceil(bar.myFloat) );
     }
 }

And I have collections of both:

 Collection < G >  fooCollection = new Collection < Foo > ();
 Collection < G >  barCollection = new Collection < Bar > ();

I want to be able to do something like this:

 Collection < G > fooCollection2 = barCollection.Convert( typeof(Foo) );

How would I go about that?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Julien-Lynge · Jan 30, 2013 at 05:10 PM 0
Share

This isn't a Unity-related question. I suggest asking this on a more general program$$anonymous$$g board, like Stack Overflow.

avatar image robinking · Jan 30, 2013 at 05:21 PM 0
Share

Oh right. Thanks, I've done that. Still, if anyone's got an answer here it'd be much appreciated.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Julien-Lynge · Jan 30, 2013 at 05:26 PM

Could you define an implicit or explicit conversion?

http://msdn.microsoft.com/en-us/library/z5z9kes2%28v=vs.71%29.aspx

Otherwise, you may just have to define a Convert method as in your example, though you'd probably need to define it as Convert< T >() - I don't remember offhand if you can pass types as arguments.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robinking · Jan 30, 2013 at 05:40 PM 0
Share

You can pass types as arguments. Do you mean add a Convert method to the Collection class, or to the Bar class? On StackOverflow someone suggested this:

 Collection<Foo> fooCollection = new Collection<Foo> { stuff = barCollection.stuff.Select(bar => (Foo)bar).ToArray() };

But I don't think that's available in .NET 2.0, is it?

avatar image Bunny83 · Jan 30, 2013 at 07:18 PM 0
Share

Sure, lambda expressions and LINQ can be used in Unity. Used it alot even on Android and iOS ;)

avatar image robinking · Jan 31, 2013 at 09:58 AM 0
Share

FanTASTic! Do you mean they can be used straight out of the box, no messing around? Someone on StackOverflow said I would need LINQBridge...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Convert a Java open source project to Unity C#? 1 Answer

Error after converting Jscript to C# 1 Answer

Error CS1061, help 1 Answer

I cant open any scripts. 0 Answers

How to Spawn objects at a certain score. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges