I have a situation where I need to know if an object is is a base class or a subclass. Looking at the docs for Introspection.IsSubclass of I thought “GREAT exactly what I need!!!”
If InfoC.IsSubclassOf(InfoD) Then
MsgBox infoC.Name + " is a subclass of "+ InfoD.Name
Else
MsgBox infoC.Name + " is NOT a subclass of "+ InfoD.Name
End if[/code]
The msg Shows “Class 1 is a subclass of class1” Which is literally false as Class1 is not a subclass if Class1!!!
I consider this a bug… but i know others often feel differently. So is this a bug that needs to be fixed or something that only needs to be documented in the language reference.
The workaround is to check if type info.BaseType is NIL
Won’t work for all cases. I would check for the class name too:
[code]Dim Object1 as New Class1
Dim Object2 As New Class1
Dim Info1 As Introspection.TypeInfo = Introspection.GetType(Object1)
Dim Info2 As Introspection.TypeInfo = Introspection.GetType(Object2)
If Info2.IsSubclassOf(Info1) And Info2.FullName <> Info1.FullName Then
MsgBox info2.Name + " is a subclass of "+ Info1.Name
Else
MsgBox info2.Name + " is NOT a subclass of "+ Info1.Name
End[/code]
I might be misunderstanding, but aren’t Object1 and Object both the same class? For Object2 to be a subclass of Object1 wouldn’t you need to create a new class based on Class1?
That’s what they’re saying. Object2 is not a subclass of Object1, they’re the same class, so TypeInfo.IsSubclassOf should be returning false but the bug is it’s returning true.
I just tried this on my Win 7, Xojo 2014R3 (I haven’t tried 3.1) and it seems to be correct. I used your exact code and I’m getting the message that “Class1 is NOT a subclass of Class1”.
[quote=156830:@Dale Arends]I just tried this on my Win 7, Xojo 2014R3 (I haven’t tried 3.1) and it seems to be correct. I used your exact code and I’m getting the message that “Class1 is NOT a subclass of Class1”.
[/quote]
That is VERY Strange… i get is a subclass on OSX 10.9.5 as well as for Windows 7 on Parrallels…
This is in a Desktop project in the default windows’s open event.
Has anybody else actually RUN my code above? If so what do you see and where are you running it?
OS X 10.10.1, I get “Class1 is a subclass of Class1” using both 2014r3.1 and 2011r3 (yes, 2011) using Karen’s original code in both a pushbutton action event and the window open event.
I get the same result with your code, which does not seem right. But then, how come Björn Dohle finds different ? Maybe he used a different code ? If this is, it would be nice to know what he used to get the appropriate response.
If InfoC.IsSubclassOf(InfoD) Then
MsgBox infoC.Name + " is a subclass of "+ InfoD.Name
Else
MsgBox infoC.Name + " is NOT a subclass of "+ InfoD.Name
End if[/quote]
This is real strange. I did the same, and both in the Open event of the window or in the button action I get Class1 is a subclass of Class1 . 2014R3.1, Yosemite 10.10.1.