Casting an object to its own type at runtime 
Author Message
 Casting an object to its own type at runtime

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?



Wed, 21 Sep 2005 05:30:09 GMT  
 Casting an object to its own type at runtime
This is called polymorphism. When you call the ID property the appropriate
class's ID property will execute, even though you are calling through a base
class reference.


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?



Wed, 21 Sep 2005 10:07:03 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Casting an Object to its own type

2. Typecasting an object to its own type at runtime

3. Type Casting Objects in VB

4. Define Object Type at Runtime

5. Question about type casting in .Net

6. Casting an array's type?

7. Cast from String to type Double is not valid

8. How to cast to interface type?

9. Help - Enumerated types and casting

10. Type casting

11. Type Casting Nightmare

12. Type Cast

 

 
Powered by phpBB® Forum Software