title: How can I check the final type of a variable?
body: <p>I have a BaseClass, a DerivedClass1 and a DerivedClass2 from a third party library. DerivedClass1 and DerivedClass2 both inherit from BaseClass.</p>
  
  <p>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.</p>
  
  <p>I want to know if ActiveItem is of DerivedClass1, as it can change in runtime without notice.</p>
  
  <p>If I do</p>
  
  <pre><code> Dim isDerivedClass1 as boolean = TypeOf(oject.ActiveItem) Is DerivedClass1 
  </code></pre>
  
  <p>then I get a compile time error, telling me that ActiveItem can never be of DerivedClass1 type.</p>
  
  <p>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.</p>
  
  <p>Is there any workaround?
  I guess I could do it with Reflection, but seems really overkill.</p>
  
  <p><strong>Edit:</strong>
  The following code doesn't compile in vs2005 SP1.</p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>Edit:</strong>
  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.</p>
  
  <p><strong>Edit:</strong>
  Again, my fault. I'm checking the <strong>Unrelated</strong> type against <strong>Base</strong>.</p>