How to tell if a class inherits another class?

D

Don

1. Say I have three classes: A, B and C.

2. Class A can only be inherited, and B inherits it. C does not inherit
anything.

3. I have a function that can have an object passed to it. Either B or C
can be passed to it.

4. In this function, I need to determine whether the object passed to it
inherits A or not.


In pseudo-code:

Function MyFunction(object)

If object.Inherits(A) Then
Return "Inherits A"
Else
Return "Does not inherit A"
End If

End Function


MyFunction(B) --> "Inherits A"
MyFunction(C) --> "Does not inherits A"


Is there any way to do this?

- Don
 
D

Don

Try the IsSubclassOf method:


Function MyFunction(object)

If object.GetType.IsSubclassOf (A) Then
Return "Inherits A"
Else
Return "Does not inherit A"
End If

End Function
 
D

Don

Thanks! That works!


Don said:
Try the IsSubclassOf method:


Function MyFunction(object)

If object.GetType.IsSubclassOf (A) Then
Return "Inherits A"
Else
Return "Does not inherit A"
End If

End Function
 

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