The following code example shows how to create an instance of a type converter and bind it to a class. The class implementing the converter, MyClassConverter, must inherit from the TypeConverter class.
|
<TypeConverter(GetType(MyClassConverter))> _
Public Class Class1
' Insert code here.
End Class 'MyClass
|
|
[TypeConverter(typeof(MyClassConverter))]
public class MyClass {
// Insert code here.
}
|
|
public:
[TypeConverter(Sample::MyClassConverter::typeid)]
ref class MyClass
{
// Insert code here.
};
|
|
/** @attribute TypeConverter(MyClassConverter.class)
*/
public class MyClass
{
// Insert code here.
} //MyClass
|
When you have a property that has an enumeration, check to see whether an enumeration value is valid before setting the property. The next code example requires that an enumeration called MyPropertyEnum has been declared.
|
Public WriteOnly Property MyProperty() As MyPropertyEnum
Set
' Checks to see if the value passed is valid.
If Not TypeDescriptor.GetConverter(GetType(MyPropertyEnum)).IsValid(value) Then
Throw New ArgumentException()
End If
' The value is valid. Insert code to set the property.
End Set
End Property
|
|
public MyPropertyEnum MyProperty {
set {
// Checks to see if the value passed is valid.
if (!TypeDescriptor.GetConverter(typeof(MyPropertyEnum)).IsValid(value)) {
throw new ArgumentException();
}
// The value is valid. Insert code to set the property.
}
}
|
|
public:
property MyPropertyEnum MyProperty
{
void set( MyPropertyEnum value )
{
// Checks to see if the value passed is valid.
if ( !TypeDescriptor::GetConverter( MyPropertyEnum::typeid )->IsValid( value ) )
{
throw gcnew ArgumentException;
}
// The value is valid. Insert code to set the property.
}
}
|
|
/** @property
*/
public void set_MyProperty(MyPropertyEnum value)
{
// Checks to see if the value passed is valid.
if (!(TypeDescriptor.GetConverter(MyPropertyEnum.class.ToType()).
IsValid(value))) {
throw new ArgumentException();
}
// The value is valid. Insert code to set the property.
} //set_MyProperty
|
Another common type converter usage is to convert an object to a string. The following code example prints out the name of the Color stored in the variable c.
|
Dim c As Color = Color.Red
Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c))
|
|
Color c = Color.Red;
Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c));
|
|
Color c = Color::Red;
Console::WriteLine( TypeDescriptor::GetConverter( c )->ConvertToString( c ) );
|
|
Color c = Color.get_Red();
Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c));
|
You can also use a type converter to convert a value from its name, as shown in the next code example.
|
Dim c As Color = CType(TypeDescriptor.GetConverter(GetType(Color)).ConvertFromString("Red"), Color)
|
|
Color c = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString("Red");
|
|
Color c = (Color)(TypeDescriptor::GetConverter( Color::typeid )->ConvertFromString( "Red" ));
|
|
Color c = (Color)(TypeDescriptor.GetConverter(Color.class.ToType()).
ConvertFromString("Red"));
|
In the following code example, you can use a type converter to print out the set of standard values that the object supports.
|
Dim c As Color
For Each c In TypeDescriptor.GetConverter(GetType(Color)).GetStandardValues()
Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c))
Next c
|
|
foreach(Color c in TypeDescriptor.GetConverter(typeof(Color)).GetStandardValues()) {
Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c));
}
|
|
for each ( Color c in TypeDescriptor::GetConverter( Color::typeid )->GetStandardValues() )
{
Console::WriteLine( TypeDescriptor::GetConverter( c )->ConvertToString( c ) );
}
|
|
IEnumerator e = TypeDescriptor.GetConverter(Color.class.ToType()).
GetStandardValues().GetEnumerator();
while (e.MoveNext()) {
Color c = (Color)e.get_Current();
Console.WriteLine(TypeDescriptor.GetConverter(c).ConvertToString(c));
}
|