| | |
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 activedirectory ado.net algorithm array barchart bitmap box broadcast buttons c# check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development disabled displayingopenforms draganddrop drawing encryption enum event excel exectuable file files form format forms ftp function gdi+ httpwebrequest image index index-error input install java label list listbox listener mandelbrot math mathematics mouseclick mysql operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting richtextbox serialization server setup sleep socket sql statistics stream string table tcp text textbox thread time timer update user usercontrol validation visualstudio webbrowser windows winforms wpf xml





