How to determine if an object class is indirectly inherited from a specific type?

J

Joergen Bech

I have written the following snippet (which works), but is there a
more "correct" way to do this? Any way of getting rid of the
loop?

TIA,

Joergen Bech

---snip---

Private Function IsSystemWindowsFormsControl(ByVal o As
Object) As Boolean
If o Is Nothing Then
Return False
Else
Return ObjectInheritsFromClass(o,
GetType(System.Windows.Forms.Control))
End If
End Function

Private Function ObjectInheritsFromClass(ByVal ctrl As Object,
ByVal ObjectType As Type) As Boolean
Dim ct As Type = ctrl.GetType
Do While Not ct Is Nothing
If ct.Equals(ObjectType) Then
Return True
Else
ct = ct.BaseType
End If
Loop
Return False
End Function
 
D

Dragon

I have written the following snippet (which works), but is there a
more "correct" way to do this? Any way of getting rid of the
loop?

A MUCH shorter way:

TypeOf o Is System.Windows.Forms.Control

8=]

Roman
 
J

Joergen Bech

Thanks. I bow my head in shame. At least I knew that there was
something I did not know. Small comfort.

/JB

I have written the following snippet (which works), but is there a
more "correct" way to do this? Any way of getting rid of the
loop?

A MUCH shorter way:

TypeOf o Is System.Windows.Forms.Control

8=]

Roman
 

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