TypeOf Problem, why doesn't this work?

B

Brien King

Ok, I have three classes (The example here is extremely simplified to
illustrate the problem) like this:

Public Class A
Public Sub DoSomething(ByVal myClass)
If TypeOf myClass IS A Then
'
' Never Enters here....
'
End If
End Sub
End Class

Public Class B
Inherits Class A
End Class

Public Class C
Inherits Class B
End Class

Then I use Class C to do some work like this:

dim myClassC1 as New C
dim myClassC2 as New C
C1.DoSomething(C2)

The problem is, is that when DoSomething is executed on the Base class, the
TypeOf check fails and says that the object passed in is NOT of the type A.
I was under the impression that the TypeOf would return True if the object
was the specific class I was asking about or any class that derived from it.

Since C derives from B which derives from A, C IS a type of A. Is this
incorrect? Or am I missing something?

Any help would be greatly appreciated!

Brien King
(e-mail address removed) (real email address)
http://www.arcaderestoration.com
 
B

Brien King

Ok, as a follow up to this, I discovered what I was doing wrong and I am
posting a reply to my own message here so that others bennifit from my
mistake.

The Code below actually DOES work (I didn't test it, I just created it to
illustrate the issue I was having). However, the problem I was running into
didn't have to do with TypeOf, it actually was a problem with my Drag & Drop
code. I had created a base class that implemented a bunch of Drag & Drop
options.

When I started my Drag & Drop I did:

Me.DoDragDrop(Me, DragDropEffects.Move)

Which is fine for the current class. However, in derived classes, this is
problematic as the e.GetData(GetType(MyBaseClass)) will return NULL for
anything other than the specific type you are looking for. This is why my
"If TypeOF e.GetData(GetType(MyBaseClass)) IS MyBaseClass" returned FALSE.
Since e.GetData(GetType(MyBaseClass)) returned NOTHING, TypeOf determined
that NOTHING is not a type of my base class. Which is True :)

So what I had to do is create a new wrapper Class that I called
TDraggedClass that was simply a container for my class. Then I could do:

tempObj = e.GetData(GetType(TDraggedClass))

and that would return my wrapper class. If it didn't then I knew it wasn't
something I was expecting and could just exit. If it returned a valid
object then I could ask it for the object it had inside of it and my TypeOf
would then work as expected.

Brien King
(e-mail address removed) (real email address)
http://www.arcaderestoration.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top