Type Conversions

by Brian Brewder May 12, 2008 00:44

Note: This article contains a sample project that you can download from the Downloads section of this website - Converting Types Code Example (download lost due to blog update). The code is described towards the end of this article.

.Net provides many different ways to convert from one type to another. The simplest way to convert one type to another is to use the System.Convert class. This class provides a number of methods to convert to common types such as Boolean, Integer, etc or to any type as long as the type you are converting from implements the IConvertible interface and defines the appropriate conversion.

You can also use TypeConverters to convert from one type to another. A TypeConverter is a class that defines a bunch of methods that allow you to convert from one type to another. TypeConverters are used extensively in the property browser control (a control that allows you to view properties of a class - used extensively in Visual Studio) and is used primarily to convert from a string to a object and back again. However, TypeConverters can also be used in your code to perform conversions.

   1:  MyTypeConvertTest tcTest = new MyTypeConvertTest("Hello from tcTest");
   2:  TypeConverter converter = TypeDescriptor.GetConverter(tcTest);
   3:  MyTest test6 = (MyTest)converter.ConvertTo(tcTest, typeof(MyTest));
   4:  Console.WriteLine(test6);

Of course this code only works if you have defined a TypeConverter for MyTypeConvertTest that can convert a MyTypeConvertTest instance to MyTest. Converters aren't too difficult to write, as long as they are simple (they can become somewhat complex when you are writing them for Visual Studio and are using some of the more advanced features).

Another common method is to provide methods on your class to convert to and from other types. For example, if you wanted to convert a string into a particular type, you might define a static Parse method on your class that takes a string and returns an instance of the class. To convert the object to another type, you can define ToXxx methods on your class (eg, ToBoolean, ToInteger, etc).

Unfortunately the System.Convert class does not use TypeConverters when doing conversions nor can it handle conversion methods so you are stuck with having to know exactly how the developer intended for the type to be converted (did they implement the IConvertible interface? a TypeConverter? conversion methods?).

Download Converting Types Code Example (download lost due to blog update)

I have created a sample project that demonstrates using the IConvertible interface, TypeDescriptors, and conversion methods. This project also provides a class that is similar to Microsoft's System.Convert.ChangeType method, but can also handle TypeDescriptors and conversion methods (Microsoft's Convert.ChangeType method only handles classes that implement the IConvertible interface).

Tags:

Powered by BlogEngine.NET 1.6.0.0

About the author

I've been a software developer since 1999 and have been working with .Net since 2002. I love creating software, playing with productivity tools, and improving the process of software development. I hope you enjoy my blog. Please feel free to leave comments or contact me, I would love to hear from you.