K
Kerry Sanders
I am working on a project for work where I need a specialized type
converter to convert the value of a string which is edited in a grid
back to the underlying object type from the cell. The value in the
cell is displayed as a string from the ToString() method in the class.
Anyway, I am trying to implement my converter, but I am having a
little trouble understanding it fully and how exactly to implement
some of the methods, especially the ConvertFrom and whether or not I
have properly implemented the CreateInstance method.
I have written some code below. This code is not the actual code, but
it gives a good representation of what I am trying to do.
Can someone give me some help on this?
public class PizzaConverter : ExpandableObjectConverter
{
public override bool
GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
public override object CreateInstance(ITypeDescriptorContext
context, IDictionary propertyValues)
{
return new Pizza((string)propertyValues["Maker"],
(int)propertyValues["NumberOfPizzas"]);
}
public override bool CanConvertFrom(ITypeDescriptorContext
context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom (context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext
context, System.Globalization.CultureInfo culture, object value)
{
}
}
[TypeConverter(typeof(PizzaConverter))]
public class Pizza
{
public Pizza(string PizzaMaker, int NumberOrdered)
{
Maker = PizzaMaker;
NumberOfPizzas = NumberOrdered;
}
private string sMaker = "";
public string Maker
{
get { return this.sMaker; }
set { this.sMaker = value; }
}
private int mNumberOfPizzas = 0;
public int NumberOfPizzas
{
get { return this.mNumberOfPizzas; }
set { this.mNumberOfPizzas = value; }
}
}
converter to convert the value of a string which is edited in a grid
back to the underlying object type from the cell. The value in the
cell is displayed as a string from the ToString() method in the class.
Anyway, I am trying to implement my converter, but I am having a
little trouble understanding it fully and how exactly to implement
some of the methods, especially the ConvertFrom and whether or not I
have properly implemented the CreateInstance method.
I have written some code below. This code is not the actual code, but
it gives a good representation of what I am trying to do.
Can someone give me some help on this?
public class PizzaConverter : ExpandableObjectConverter
{
public override bool
GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
public override object CreateInstance(ITypeDescriptorContext
context, IDictionary propertyValues)
{
return new Pizza((string)propertyValues["Maker"],
(int)propertyValues["NumberOfPizzas"]);
}
public override bool CanConvertFrom(ITypeDescriptorContext
context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom (context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext
context, System.Globalization.CultureInfo culture, object value)
{
}
}
[TypeConverter(typeof(PizzaConverter))]
public class Pizza
{
public Pizza(string PizzaMaker, int NumberOrdered)
{
Maker = PizzaMaker;
NumberOfPizzas = NumberOrdered;
}
private string sMaker = "";
public string Maker
{
get { return this.sMaker; }
set { this.sMaker = value; }
}
private int mNumberOfPizzas = 0;
public int NumberOfPizzas
{
get { return this.mNumberOfPizzas; }
set { this.mNumberOfPizzas = value; }
}
}