Dealing with different types of objects in an ArrayList

K

Klerk

I have a base class Control and a 2 derived classes Control<T>:Control
and ControlEnum:Control.

The reason I have Control<T> and ControlEnum is these classes have a
field called "value" whose type is unknown until runtime. If it's type
is an enum, the enum definition itself isnt known until run time.

So at runtime I am basically doing the following:
1. get type of value
2. if int create Control<int>
if float create Control<float> etc
if enum build "list" of names and values and create ControlEnum
(passing names[] values[] to its ctor)

I initially had the field "value" defined in the base class, but moved
it into the two subclass as I couldnt come up with a way of
dynamically creating an enum and passing it as T to Control<T>.

Now I have an ArrayList of controls and want to be able to access
"value" like so

foreach(control c in controls)
tmp = c.Value

and here I get stuck not knowing what the type of c.Value is!
How do I design the classes to handle this? I am newbie to oops
concepts so any advice would be greatly appreciated. Thanks a lot.
 
I

Ignacio Machin ( .NET/ C# MVP )

I have a base class Control and a 2 derived classes Control<T>:Control
and ControlEnum:Control.

The reason I have Control<T> and ControlEnum is these classes have a
field called "value" whose type is unknown until runtime. If it's type
is an enum, the enum definition itself isnt known until run time.

So at runtime I am basically doing the following:
1. get type of value
2. if int create Control<int>
   if float create Control<float> etc
   if enum build "list" of names and values and create ControlEnum
(passing names[] values[] to its ctor)

I initially had the field "value" defined in the base class, but moved
it into the two subclass as I couldnt come up with a way of
dynamically creating an enum and passing it as T to Control<T>.

Now I have an ArrayList of controls and want to be able to access
"value" like so

foreach(control c in controls)
    tmp = c.Value

and here I get stuck not knowing what the type of c.Value is!
How do I design the classes to handle this? I am newbie to oops
concepts so any advice would be greatly appreciated. Thanks a lot.

Hi,

Declare an interface with the common properties and have the classes
implement it. Then you can have a List<MyInterface>
 
N

Nicholas Paldino [.NET/C# MVP]

Klerk,

I'm still a little confused about why you aren't using Control<Enum> for
your enumeration. What do you mean you couldn't come up with a way to
create the enum and pass it as T? Why pass the names and values to the
constructor? Is this what is preventing you from putting the Value property
on the base class?
 
K

Klerk

Klerk,

    I'm still a little confused about why you aren't using Control<Enum> for
your enumeration.  What do you mean you couldn't come up with a way to
create the enum and pass it as T?  Why pass the names and values to the
constructor?  Is this what is preventing you from putting the Value property
on the base class?

Yes, this is the reason I moved Value out of the base class. The cause
of my problems dealing with the array list goes back to not being able
to dynamically(at run time) create an enum that could then be passed
to Control<T>.

I could use control<Enum> if an enum type was defined at compile time,
but the problem is I only find out at run time, (1)what controls are
available AND (2)the _type_ of their "values" (enum being a special
case). Basically I get some xml from a device describing its controls.
In case a control's value is of type enum, the xml would also provide
a description(names and values) of the enum. So at runtime I need some
way of creating the enum type.

I tried using reflection emit enumbuilder to create the type. There is
a Enumbuilder.createType function, I tried using this based on the
msdn example(VS2005/.net2.0).
So my code looked like:
if type in xml == enum
....
Type dynamicEnum = Enumbuilder.createType()
Control<dynamicEnum> c = new Control<dynamicEnum>(...) << But the
compiler complains about dynamicEnum here.

So I guess I have two question
1)Is it possible to create a enum at runtime and use it as a parameter
to a generic type?

2)Whats a good pattern of traversing a list with different types of
objects
I ended up doing the following(which worked) but is this the best way
to do this?
foreach (Object c in controls)
{
Object val = c.GetType().InvokeMember("Value",
BindingFlags.GetProperty, null, c, null);
}

Thanks for the replies and your patience with my convoluted
explanations...
 

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