NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Implementing a Type Converter

A type converter allows a given data type to be converted to another data type. Type converters are primarily used for string-to-value conversions and for validation at design-time and at run time. In a host such as a property window in the forms designer, type converters allow a property value to be represented as text to the user, and convert text entered by the user to the appropriate data type.

Most native data types (Int32, string, enumeration types, and others) have default type converters that provide string-to-value conversions and perform validity checks. The default type converters are in the System.ComponentModel namespace and are named <TypeConverterName>Converter. You must implement a custom type converter when the default functionality is not adequate, or when you define a custom type that does not have an associated type converter.

Note
A TypeConverterAttribute must be applied to a property or a data type to associate it with a custom type converter.
The implementation of a type converter is independent of any user interface functionality. Hence any type converter can be applied to both Win Forms properties and Web Forms properties.

To implement a simple type converter

The following code example implements a type converter that converts from a string type to a System.Drawing.Point type and from System.Drawing.Point to string. The CanConvertTo and IsValid methods listed above are not overriden in this example.

[C#]
using System;
using System.ComponentModel;

public class PointConverter : TypeConverter {

//Overrides the CanConvertFrom method of TypeConverter
// The ITypeDescritorContext interface provides the context for the
// conversion. Typically this interface is used at design time to 
// provide information about the design time container.
   public override bool CanConvertFrom(ITypeDescriptorContext context, 
               Type sourceType) {

      if (sourceType == typeof(string)) {
         return true;
      }
      return base.CanConvertFrom(context, sourceType);
   }
//Overrides the ConvertFrom method of TypeConverter.
   public override object ConvertFrom(ITypeDescriptorContext context, 
               object value, 
               object[] arguments) {

      if (value is string) {
         string[] v = ((string)value).Split(new char[] {‘,’});
         return new Point(int.Parse(v[0]), int.Parse(v[1]));
      }
      return base.ConvertFrom(ITypeDescriptorContext context, 
               object value, 
               object[] arguments);
   }
//Overrides the ConvertTo method of TypeConverter
public override object ConvertTo(ITypeDescriptorContext context, 
               object value, 
               Type destinationType, 
               object[] arguments) {

      if (destinationType == typeof(string)) {
         return ((Point)value).X + “,” + ((Point)value).Y;
      }
      return base.ConvertTo(ITypeDescriptorContext context, 
               object value, 
               Type destinationType, 
               object[] arguments);
   }
}



A complete sample is presented in Type Converter Sample.