Posted by: Zeeshan Amjad | August 12, 2009

Using TypeConvertor in C#


Type converter is not a new concept in .Net. In fact it is a part of .Net since its very first version i.e. .Net 1.0. The basic purpose of TypeConvertor is to convert from one data type to another. There is a class TypeConvertor directly inherited by Object class.

TypeConverter

.Net class library defined hundreds of type converter inherited by this class.

One of the usage of TypeConvertor classes in WPF can be seen in XAML. In XAML we usually set the properties in the form of String. Such as set the background color, margin, padding, font etc. But behind the scene it is type convertor that convert the string into appropriate data type.

Here is one simple example of Border setting the value of some of its properties.

  1: <Window x:Class="xaml.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     xmlns:local="clr-namespace:xaml"
  5:     Title="Type Convertor Demo" Height="400" Width="400">
  6: 
  7:     <Border Margin="10" Background="Blue" BorderThickness="10" BorderBrush="Yellow">
  8:         
  9:     </Border>
 10: </Window>
 11: 

If we want to do the same thing in C# code then we have to call the appropriate TypeConvertor to convert string into destination data type. Here is one such example.

  1: border.Margin = (Thickness)TypeDescriptor.GetConverter(typeof(Thickness)).ConvertFromString("10");

Here is the complete C# program.

  1: using System;
  2: using System.Windows;
  3: using System.Windows.Controls;
  4: using System.Windows.Media;
  5: using System.ComponentModel;
  6: 
  7: namespace TypeConverterDemo
  8: {
  9:     public class MyWindow : Window
 10:     {        
 11:         public MyWindow()
 12:         {
 13:             try
 14:             {
 15:                 Title = "Type Convertor Demo";
 16:                 Width = 400;
 17:                 Height = 300;
 18: 
 19:                 Border border = new Border();
 20: 
 21:                 border.Margin = (Thickness)TypeDescriptor.GetConverter(typeof(Thickness)).ConvertFromString("10");
 22:                 border.BorderThickness = (Thickness)TypeDescriptor.GetConverter(typeof(Thickness)).ConvertFromString("10");
 23: 
 24:                 border.Background = (Brush)TypeDescriptor.GetConverter(typeof(Brush)).ConvertFromString("Blue");
 25:                 border.BorderBrush = (Brush)TypeDescriptor.GetConverter(typeof(Brush)).ConvertFromString("Yellow");
 26: 
 27:                 Content = border;
 28:             }
 29:             catch (FormatException fe)
 30:             {
 31:                 MessageBox.Show(fe.Message, fe.Source);
 32:             }
 33:         }
 34:     }
 35: 
 36:     class Program
 37:     {
 38:         [STAThread]
 39:         static void Main(string[] args)
 40:         {
 41:             MyWindow win = new MyWindow();
 42: 
 43:             Application app = new Application();
 44:             app.Run(win);
 45:         }
 46:     }
 47: }
 48: 

Here is the output of this program.

TypeConverterOutput

There are two functions in TypeConvertor named “ConvertFromString” and “ConvertFromInvariantString”. The main difference between these two is first one is culture sensitive and the second one is not culture sensitive.

let’s explore these with example. We are going to change the Thickness little bit and passed “10,0” instead of “10” something like this.

  1: border.Margin = (Thickness)TypeDescriptor.GetConverter(typeof(Thickness)).ConvertFromString("10,0");
  2: border.BorderThickness = (Thickness)TypeDescriptor.GetConverter(typeof(Thickness)).ConvertFromString("10,0");
  3: 

Now it will call the constructor of Thickness class with two parameters. Here is the complete program of this.

  1: using System;
  2: using System.Windows;
  3: using System.Windows.Controls;
  4: using System.Windows.Media;
  5: using System.ComponentModel;
  6: using System.Globalization;
  7: 
  8: namespace TypeConverterDemo
  9: {
 10:     public class MyWindow : Window
 11:     {        
 12:         public MyWindow()
 13:         {
 14:             try
 15:             {
 16:                 Title = "Type Convertor Demo";
 17:                 Width = 400;
 18:                 Height = 300;
 19: 
 20:                 Border border = new Border();
 21: 
 22:                 border.Margin = (Thickness)TypeDescriptor.GetConverter(typeof(Thickness)).ConvertFromString("10,0");
 23:                 border.BorderThickness = (Thickness)TypeDescriptor.GetConverter(typeof(Thickness)).ConvertFromString("10,0");
 24: 
 25:                 border.Background = (Brush)TypeDescriptor.GetConverter(typeof(Brush)).ConvertFromString("Blue");
 26:                 border.BorderBrush = (Brush)TypeDescriptor.GetConverter(typeof(Brush)).ConvertFromString("Yellow");
 27: 
 28:                 Content = border;
 29:             }
 30:             catch (FormatException fe)
 31:             {
 32:                 MessageBox.Show(fe.Message, fe.Source);
 33:             }
 34:         }
 35:     }
 36: 
 37:     class Program
 38:     {
 39:         [STAThread]
 40:         static void Main(string[] args)
 41:         {
 42:             MyWindow win = new MyWindow();
 43: 
 44:             Application app = new Application();
 45:             app.Run(win);
 46:         }
 47:     }
 48: }
 49: 

The output of the program will be something like this.

TypeConverterOutput_02

Now lets do one experiment, change the language setting to “German”. In German language “,” is used for decimal point and it will treat as 10.0 in English culture. It means it will call only one parameter of Thickness and its output will be same as our first program.

Internally “ConvertFromInvarintString” calls the CultureInfo with InvariantCulture property. InvariantCulture property is associated with English language but not with any country or region. In other words it will display the information in cultural independent form.

One possible use of InvariantCulture is when we are going to make an application that can be used with different culture. In that case we may store information in cultural independent form and when retrieving data then display it in appropriate culture.

Advertisements

Responses

  1. […] Command Converter for built in commands We have already seen the example of using type converter. We have already seen the usage of built in commands using command target. Now we are going to make […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: