Link to home
Start Free TrialLog in
Avatar of riceman0
riceman0

asked on

How to tell type of class?

Hi, how do i tell if an object is of a derived class?  See the code below, what should replace "istypeof"?

Thanks very much.
Public Class A
 
    End Class
 
    Public Class B
        Inherits A
 
    End Class
 
    Public Class C
        Inherits A
    End Class
***
        Dim Lst As New List(Of A)
 
        Dim x As New B
        Lst.Add(x)
 
        if lst(0) istypeof B then
        End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of TSmooth
TSmooth

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of riceman0
riceman0

ASKER

Interesting, those both work.  There are two totally different syntaxes for this?
Possibly more. In programming there are often several ways to accomplish the same thing. Often there are tradeoffs between various methods but sometimes it's just a matter of personal preference.
Hi riceman0;

Please note the comments in snippet below the use of the typeOf ... Is operator and the use of Object.GetType() method and the GetType(type) operator they really do different things.

Fernando
Dim Lst As New List(Of A)
 
Dim x As New B
Lst.Add(x)
 
' TypeOf .. Is Operator
If TypeOf (Lst(0)) Is A Then
    ' The first argument, TypeOf (Lst(0)), is a A object or a 
    ' object that derives from A. In this case B derives from A
    ' and therefore when you compare that to a A type we have a 
    ' true statement and this statement will execute.
    Console.WriteLine("Object is of type A")
End If
 
If TypeOf (Lst(0)) Is B Then
    ' The first argument, TypeOf (Lst(0)), is a A object or a 
    ' object that derives from A. In this case B is a B type
    ' and therefore when you compare that to a B type we have a 
    ' true statement and this statement will execute.
    Console.WriteLine("Object is of type B")
End If
 
If TypeOf (Lst(0)) Is C Then
    ' The first argument, TypeOf (Lst(0)), is a B type but B neither
    ' derives or is a C type and so this statement does not execute
    Console.WriteLine("Object is of type C")
End If
 
' Object.GetType, method derived from Object and GetType(type) Operator
If Lst(0).GetType() Is GetType(A) Then
    ' Lst(0).GetType() is of type B compare that to type A and they are 
    ' Not the same and so this statement will not execute.
    Console.WriteLine("Lst(0) is of type A")
End If
 
If Lst(0).GetType() Is GetType(B) Then
    ' Lst(0).GetType() is of type B compare that to type B and they are 
    ' the same and so this statement will execute.
    Console.WriteLine("Lst(0) is of type B")
End If
 
If Lst(0).GetType() Is GetType(C) Then
    ' Lst(0).GetType() is of type B compare that to type C and they are 
    ' Not the same and so this statement will not execute.
    Console.WriteLine("Lst(0) is of type C")
End If
 
' Cast the type of Lst(0) to type A and add it to the list
Dim y As A = DirectCast(Lst(0), A)
Lst.Add(y)
 
' Object.GetType, method derived from Object and GetType(type) Operator
If Lst(1).GetType() Is GetType(A) Then
    ' Lst(0).GetType() was created as a type B but cast to type A now and 
    ' compare that to type A and they are not the same and so this 
    ' will still not execute. Casting does not cause GetType to report
    ' anything other then what the object was created like.
    Console.WriteLine("Lst(1) is of type A")
End If

Open in new window