Creating a property of a class that can be of multiple types

HeadNoodle

New member
Joined
Apr 10, 2006
Messages
3
Programming Experience
10+
:confused: Hi Guys, I am new to this website so I apologise in advance if I have posted this to the wrong forum, I couldn't find a more relevent one.

I have a class called outboundrouting that has a property named parameter. The parameter property needs to be able to hold that contents of one of 3 seperate classes (param_timeofDay,param_RatioRouting,Param_CLIRouting).

I really dont want to create 3 different propertys for the class outboundrouting so does anyone know of a standard way to do this?

Thanks in Advance,

Dean.
 
Box to Object if the classes are different, or baseclass type if they are all derived from same class.
 
if i use the baseclass isnt it true to say that i will only have access to the properties and methods in the baseclass and not the derived if they have been added to?

also what do you mean by 'box to Object'?

:confused:

Update
I've just tested the baseclass option and it worked perfectly. I'm sure when I've tested it previously in .net 2003 it only exposed the baseclass properties and methods. Oh well I learn something new everyday. Thanks for you help John. I would still like to know what box to object means though?

Thanks

Dean
 
Last edited:
Boxing means putting an object into a bigger box, Object type is the biggest 'box' in .Net, anything can be put into a variable of type Object, and that anything will still be an object of some specific type when contained, you just have to reveal its true type. A derived class object can be stored in a variable of type baseclass, this is also 'boxing', but more limited, only base or derived objects will fit into such type box.
Boxing also implies some kind of 'unboxing' when using specific type from a more general type object/collection, usually CType, DirectCast, or 'For each derObj as derivedclass in baseclasstypearray'. There are also tools such as GetType and TypeOf for these kind of operations.
 
Just to concur with what John said.

The most "sound" way is to use a base class

The easy way is to return an object and in the calling function
do a Select Case statement using the GetType of the object to
decide what type of class was returned.
 
Back
Top