views:

43

answers:

3

I have a BaseClass, a DerivedClass1 and a DerivedClass2 from a third party library. DerivedClass1 and DerivedClass2 both inherit from BaseClass.

There's a ContainerClass, from the same library, with a member variable ActiveItem, which can be of DerivedClass1 or DerivedClass2, so it is declared as BaseClass.

I want to know if ActiveItem is of DerivedClass1, as it can change in runtime without notice.

If I do

 Dim isDerivedClass1 as boolean = TypeOf(oject.ActiveItem) Is DerivedClass1 

then I get a compile time error, telling me that ActiveItem can never be of DerivedClass1 type.

I have tried several combinations of GetType and TypeOf but it doesn't seem possible to check this. I have also tried to declare an auxiliary DerivedClass1 variable and comparing their types, but haven't got any luck either.

Is there any workaround? I guess I could do it with Reflection, but seems really overkill.

Edit: The following code doesn't compile in vs2005 SP1.

Public Class Base
    Public x As Integer
End Class
Public Class Derived1
Inherits Base
    Public y As Integer
End Class
Public Class Derived2
Inherits Base
    Public z As Integer
End Class
Public Class Unrelated
    Public var As Base
End Class


Public Class Form1
    Public Sub Test(ByVal obj As Unrelated)
        Dim tst As Boolean
        tst = TypeOf obj Is Derived1
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim obj As New Unrelated
        obj.var = New Derived1
        Test(obj)
    End Sub
End Class

Edit: It seems that the original problem was a fault in my side. I was checking against the wrong type (those silly third part libraries...) However, I'm still trying to find the error in the code above.

Edit: Again, my fault. I'm checking the Unrelated type against Base.

+1  A: 

You code seems to be almost exactly right.

I've done this, which works fine:

Dim isDerivedClass1 As Boolean = TypeOf oject.ActiveItem Is DerivedClass1
Dim isDerivedClass2 As Boolean = TypeOf oject.ActiveItem Is DerivedClass2

Have I missed something?

EDIT: I think you just missed the var property in your edited code.

Public Sub Test(ByVal obj As Unrelated)
    Dim tst As Boolean
    tst = TypeOf obj.var Is Derived1
End Sub
Enigmativity
I don't know. I've added some sample code which vs2005 doesn't even compile.
Jaime Pardos
Thank you, although I had already found about the missing 'var' and posted it as a comment to my question. I'm accepting your answer anyway.
Jaime Pardos
@`Jamie Pardos` - Much appreciated. I hope I helped.
Enigmativity
+1  A: 

A simple possibility could be to use TryCast:

Dim isDerivedClass1 As Boolean = TryCast(object.ActiveItem, DerivedClass1) IsNot Nothing
Bobby
Seems promising. I'm trying to think of possible problems with this approach.
Jaime Pardos
+2  A: 

You'll have to trust the compiler on this, it is convinced that DerivedClass1 does not inherit BaseClass. It doesn't get that wrong. That's either because it didn't see the Inherits clause in the DerivedClass1 declaration or because it picked a BaseClass definition from another assembly.

To fix the former problem, you have no alternative but to declare the ActiveItem as Object or to find another type that these classes have in common. Use the Object Browser. To fix the latter problem you'll have to change the Imports directive or spell out the full name of the BaseClass type (including namespace).

Hans Passant
It seems that you are right, and the inheritance tree is not exactly as I thought it was. I have fixed my former problem. However, I still don't understand why the code above doesn't work.
Jaime Pardos
Well, because the inheritance tree is not exactly as you thought it was. I can't guess beyond that with the provided info.
Hans Passant
I meant the code I posted, which was self-contained, and as I stated in a comment it had an obvious (now) bug. Sorry for any inconvenience, and thank you for your help.
Jaime Pardos