943,614 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 5536
  • C# RSS
Apr 1st, 2009
0

How to use custom type converter to convert 'implicitly'

Expand Post »
Is it possible to use custom type converter to convert types like that:
C# Syntax (Toggle Plain Text)
  1. class MyTypeConverter: TypeConverter
  2. {
  3. //... all the 'converting' methods here
  4. }
  5.  
  6. [TypeConverter(typeof(MyTypeConverter))]
  7. class Type1 {}
  8.  
  9. class Type2 {}
  10.  
  11. Type2 object2;
  12.  
  13. //and here, is this possible? (and how to make it happen)
  14. Type1 object1 = object2;
  15.  
  16. //or at least how to make this work:
  17. Type1 object1 = (Type1)object2;

Compiler complains that it cannot convert Type1 to Type2 either way.
I can of course instantiate the converter manually and then use it's ConvertFrom method, but am I actually supposed to do that?

Please help...
Reputation Points: 10
Solved Threads: 0
Junior Poster
edek is offline Offline
113 posts
since Oct 2006
Apr 1st, 2009
0

Re: How to use custom type converter to convert 'implicitly'

I'm pretty sure the only way is to define an implicit conversion operator:
http://msdn.microsoft.com/en-us/library/z5z9kes2.aspx

Of course, it should be stressed: "Use it to enable implicit conversions between a user-defined type and another type, if the conversion is guaranteed not to cause a loss of data."

In fact, I think you shouldn't use implicit or explicit type conversion operators at all.
Reputation Points: 231
Solved Threads: 12
Junior Poster
thoughtcoder is offline Offline
139 posts
since Mar 2009
Apr 1st, 2009
0

Re: How to use custom type converter to convert 'implicitly'

In fact, I think you shouldn't use implicit or explicit type conversion operators at all.
Why? What would you suggest instead?
Last edited by edek; Apr 1st, 2009 at 4:37 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster
edek is offline Offline
113 posts
since Oct 2006
Apr 1st, 2009
0

Re: How to use custom type converter to convert 'implicitly'

Click to Expand / Collapse  Quote originally posted by edek ...
Why? What would you suggest instead?
Functions.
Reputation Points: 231
Solved Threads: 12
Junior Poster
thoughtcoder is offline Offline
139 posts
since Mar 2009
Apr 1st, 2009
0

Re: How to use custom type converter to convert 'implicitly'

Functions.
Ok, the only problem with that is that it is (i believe) not a usual way to do this. I might forget that the project used functions for conversion, and if someone else did need to use libraries written by me, they might not even think about the possibility that functions were used for conversion. Am I right? Is this good enough reason to use implicit/explicit conversion instead? Are there any other pros of using functions?
Last edited by edek; Apr 1st, 2009 at 10:03 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster
edek is offline Offline
113 posts
since Oct 2006
Apr 1st, 2009
1

Re: How to use custom type converter to convert 'implicitly'

Click to Expand / Collapse  Quote originally posted by edek ...
Ok, the only problem with that is that it is (i believe) not a usual way to do this. I might forget that the project used functions for conversion, and if someone else did need to use libraries written by me, they might not even think about the possibility that functions were used for conversion. Am I right?
No. I would expect people to use plain old functions. There are large downsides to explicit conversion operators -- mainly that using them makes it hard to identify the places where you're using run-time casts, because they look the same. Implicit conversion operators make it difficult to read code outside of the IDE. Which we do at my workplace, in code reviews and when we don't want to open up a file from another branch in a whole new IDE instance. Using plain old functions, calling them with x.ToBar() or Bar.FromFoo(x) or possibly new Bar(x) doesn't have either disadvantage. Since using plain old functions reduces the probability of making an error, it makes sense to use them.

On the other hand, there might be advantages to using the TypeConverter framework that I'm not familiar with. But converting between equivalent types isn't exactly a hard problem.
Reputation Points: 231
Solved Threads: 12
Junior Poster
thoughtcoder is offline Offline
139 posts
since Mar 2009
Apr 1st, 2009
0

Re: How to use custom type converter to convert 'implicitly'

Thanks for explanation. I'll use functions - I've found that my types are not so 'equivalent' as I thought. Thanks again!
Reputation Points: 10
Solved Threads: 0
Junior Poster
edek is offline Offline
113 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Adding icon into an exe file
Next Thread in C# Forum Timeline: Settings.settings thing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC