how to tell which base class

  • Thread starter Thread starter mrrrk
  • Start date Start date
M

mrrrk

Supposing I have a base (abstract) class called thingBase and then I create
two more abstract classes from it, e.g. thingBase1, thingBase2.

I have code that operates on thingBase objects but I need to know whether
the object is inherited from thingBase1 or thingBase2.

How do I tell which base class was used?
 
* "mrrrk said:
Supposing I have a base (abstract) class called thingBase and then I create
two more abstract classes from it, e.g. thingBase1, thingBase2.

I have code that operates on thingBase objects but I need to know whether
the object is inherited from thingBase1 or thingBase2.

'ThingBase1' and 'ThingBase2' _inherit_ from 'ThingBase'?
 
mrrrk said:
Supposing I have a base (abstract) class called thingBase and then I
create two more abstract classes from it, e.g. thingBase1,
thingBase2.

I have code that operates on thingBase objects but I need to know
whether the object is inherited from thingBase1 or thingBase2.

How do I tell which base class was used?

If TypeOf argument is thingBase1 then
dim thing as thingbase1
thing = directcast(thing, thingbase1)
'access type specific members here
'...
elseif typeof argument is thingBase2 then
dim thing as thingbase2
thing = directcast(thing, thingbase2)
'access type specific members here
'...
end if
 
mrrrk,
I normally use the TypeOf operator for this.

Dim thing as thingBase

If TypeOf thing Is thingBase1 Then
Dim thing1 As thingBase1 = DirectCast(thing, thingBase1)

ElseIf TypeOf thing Is thingBase2 Then
Dim thing2 As thingBase2 = DirectCast(thing, thingBase2)


End If

However this should be the exception and not the norm!!!!

If you have code that behaves differently based on thingBase1 or thingBase2,
you should consider making that code an overridable method of thingBase and
have thingBase1 & thingBase2 override the method with their own code! Which
is Polymorphism & one of the reasons for inheritance.

Hope this helps
Jay
 
Back
Top