| | |
How to use custom type converter to convert 'implicitly'
Thread Solved |
Is it possible to use custom type converter to convert types like that:
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...
C# Syntax (Toggle Plain Text)
class MyTypeConverter: TypeConverter { //... all the 'converting' methods here } [TypeConverter(typeof(MyTypeConverter))] class Type1 {} class Type2 {} Type2 object2; //and here, is this possible? (and how to make it happen) Type1 object1 = object2; //or at least how to make this work: 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...
•
•
Join Date: Mar 2009
Posts: 139
Reputation:
Solved Threads: 12
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.
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.
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.
•
•
Join Date: Mar 2009
Posts: 139
Reputation:
Solved Threads: 12
•
•
•
•
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?
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.
![]() |
Other Threads in the C# Forum
- Previous Thread: Adding icon into an exe file
- Next Thread: Settings.settings thing
Views: 1997 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C#
.net 2d access algorithm application array asp.net bitmap box button c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees deployment drawing enum excel file files form format forms function gcd gdi+ graphics grid http httpwebrequest image index input interface java list listbox login mandelbrot math mysql networking notepad operator path pda picturebox pixelinversion prime print programming regex remote remoting resource resourcefile save saving search serialization server sleep socket sounds sql statistics string tcp text textbox thread time timer treeview update usercontrol validation view visual visualstudio webbrowser webcam windows winforms wpf xml





