Typecasting an object to its own type at runtime 
Author Message
 Typecasting 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:14:55 GMT  
 Typecasting an object to its own type at runtime
Hello Tobe,


Quote:
> When assigning an object to a variable of the class's
> base class it uses the methods of the base class.

Read Chris' reply to the other "instance" of your posting.

;-)

Regards,
Herfried K. Wagner



Wed, 21 Sep 2005 05:25:48 GMT  
 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.

False.  If a class Overrides a method in the base class, then the derived
class' method will be called whether on the base class reference or a child
class reference.  Only if the mehtod is declared as Shadows will it behave
as described.
Module Module1

  Public Class clsItemBase
    Public Overridable Function ID() As String
      Return "base class ID"
    End Function
  End Class

  Public Class clsDealer
    Inherits clsItemBase
    Public Overrides Function ID() As String
      Return "clsDealer ID"
    End Function
  End Class

  Public Class clsMaster
    Inherits clsItemBase
    Public Overrides Function ID() As String
      Return "clsMaster ID"
    End Function
  End Class
  Sub Main()
    Dim D As clsItemBase = New clsDealer()
    Dim M As clsItemBase = New clsMaster()

    Console.WriteLine(D.ID)
    Console.WriteLine(DirectCast(D, clsDealer).ID)
    Console.WriteLine(M.ID)
    Console.WriteLine(DirectCast(M, clsMaster).ID)

    'Outputs
    'clsDealer ID
    'clsDealer ID
    'clsMaster ID
    'clsMaster ID

  End Sub

End Module



Wed, 21 Sep 2005 05:31:25 GMT  
 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



Wed, 21 Sep 2005 05:35:33 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Casting an object to its own type at runtime

2. Casting an Object to its own type

3. typecast object to other interface

4. typecast object to other interface

5. typecast object to other interface

6. typecasting an object

7. Define Object Type at Runtime

8. write runtime error to own log

9. write runtime error to own log

10. Textbox Resizes on its own at Runtime

11. instance of class module can't see own type

12. Section breaks change type--on their own!

 

 
Powered by phpBB® Forum Software