Polymorphic property How?

G

Gerald

Hi,

suppose i have a simple class consist of two properties

ValueType which is an enum consisting of
(integer,boolean,string,datetime,color)

And a property called Value which is of type object

I want Visual studio 2005 to display a coloreditor for property Value
when Valuetype == color, a datepicker when ValueType==datetime etc..

so in essence the type of Value changes according to the value of the
property ValueType,


What is the best way to go about this. I looked at
TypeConverter,UITypeEditor and TypeDescriptionProvider the latter seems
most promissing, but can't find any good material about it.


--
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Gerald said:
Hi,

suppose i have a simple class consist of two properties

ValueType which is an enum consisting of
(integer,boolean,string,datetime,color)

You mean that the property can return any of these types??
I think you could use a Type better. this allow you more flexibility.
And a property called Value which is of type object

I want Visual studio 2005 to display a coloreditor for property Value
when Valuetype == color, a datepicker when ValueType==datetime etc..

so in essence the type of Value changes according to the value of the
property ValueType,

Question, did you consider using a generic class instead?

class XXX<T>
{
T instance;
Type ValueType{ get{ return typeof(T);} }
T Value{ get{return instance;} }
}

You are using 2005 after all
What is the best way to go about this. I looked at
TypeConverter,UITypeEditor and TypeDescriptionProvider the latter seems
most promissing, but can't find any good material about it.

You could use a factory pattern, define a common interface like
IDynamicControl and have several classes implement it each will handle a
different type of variable. Then you have your factory method that will
receive an instance of XXX and create the correct implementation of
IDynamicControl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top