
Typecasting an object to its own type at runtime
Quote:
> When assigning an object to a variable of the class's
> base class it uses the methods of the base class.
> dim D as clsItemBase = new clsDealer()
> dim M as clsItemBase = new clsMaster()
> These classes both have an ID property, but they are in
> different formats, so we need to cast them on the fly to
> the proper types. We need to do something like this:
> debug.writeline( CType(d, d.gettype).ID.tostring() )
> debug.writeline( CType(m, m.gettype).ID.tostring() )
> CType is alwasy early binding, so it doesn't allow this.
> How can we do it?
You've got a logical problem here. ;-)
You do type casting because you do know the type of the object at *design
time*. You don't have to determine it at run time. As you want to access a
property declared in a specific class, you already know the object's type,
and therefore directly write the type name:
debug.writeline( Directcast(d, clsDealer).ID.tostring() )
debug.writeline( Directcast(m, clsMaster).ID.tostring() )
Both classes only "happens to have" a property with the same name, but this
doesn't matter.
Apart from that, you can declare D as clsDealer and M as clsMaster. ;-)
Armin