| | |
How to use custom type converter to convert 'implicitly'
Please support our C# advertiser: Intel Parallel Studio Home
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
| Thread Tools | Search this Thread |
.net access algorithm array asp barchart bitmap box broadcast c# check checkbox client column combobox control conversion csharp custom database databasesearch datagrid datagridview datagridviewcheckbox dataset datetime degrees development display draganddrop drawing encryption enum equation excel file form format formatting formbox forms formupdate function gdi+ image index input install java label linux list listbox mandelbrot math mouseclick mysql namevaluepairs networking operator packaging path photoshop picturebox pixelinversion post powerpacks programming radians regex remoting reporting richtextbox robot server sleep socket sql statistics stream string table text textbox thread time timer transform treeview update usercontrol validation visualstudio wait webbrowser wfa windows winforms wpf xml





