How to use custom type converter to convert 'implicitly'

Thread Solved

Join Date: Oct 2006
Posts: 113
Reputation: edek is an unknown quantity at this point 
Solved Threads: 0
edek's Avatar
edek edek is offline Offline
Junior Poster

How to use custom type converter to convert 'implicitly'

 
0
  #1
Apr 1st, 2009
Is it possible to use custom type converter to convert types like that:
  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...
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 139
Reputation: thoughtcoder is on a distinguished road 
Solved Threads: 12
thoughtcoder thoughtcoder is offline Offline
Junior Poster

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

 
0
  #2
Apr 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 113
Reputation: edek is an unknown quantity at this point 
Solved Threads: 0
edek's Avatar
edek edek is offline Offline
Junior Poster

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

 
0
  #3
Apr 1st, 2009
Originally Posted by thoughtcoder View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 139
Reputation: thoughtcoder is on a distinguished road 
Solved Threads: 12
thoughtcoder thoughtcoder is offline Offline
Junior Poster

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

 
0
  #4
Apr 1st, 2009
Originally Posted by edek View Post
Why? What would you suggest instead?
Functions.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 113
Reputation: edek is an unknown quantity at this point 
Solved Threads: 0
edek's Avatar
edek edek is offline Offline
Junior Poster

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

 
0
  #5
Apr 1st, 2009
Originally Posted by thoughtcoder View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 139
Reputation: thoughtcoder is on a distinguished road 
Solved Threads: 12
thoughtcoder thoughtcoder is offline Offline
Junior Poster

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

 
1
  #6
Apr 1st, 2009
Originally Posted by edek View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 113
Reputation: edek is an unknown quantity at this point 
Solved Threads: 0
edek's Avatar
edek edek is offline Offline
Junior Poster

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

 
0
  #7
Apr 1st, 2009
Thanks for explanation. I'll use functions - I've found that my types are not so 'equivalent' as I thought. Thanks again!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum


Views: 1997 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC