Is there a HasA function which exists for determining the property of an object?
There is an IsA function which determines that an object IsA Class.
PseudoCode - not tested
MySpeed as Integer
The class is instantiated and the IsA function works. Here is how a theoretical HasA function would work:
[code]Dim MyCarClass as New MyCar
If MyCarClass IsA MyCar then //This works
msgbox “Yes, MyCarClass is from the class MyCar”
If MyCarClass HasA MySpeed then //Does a form of this exist?
msgbox “Yes, MyCarClass has a property called MySpeed”
I couldn’t seem to find this in the documentation and not sure if someone else has experienced this.
Markus is right about Introspection, but you might want to look into Class Interfaces too.
Not sure why you’d want to check if a property exists, but Introspection is the way. Here’s a quick method that might help:
[code]Function HasProperty(Extends o As Object, name As Text) As Boolean
Dim info As TypeInfo = GetType(o)
Dim props() As PropertyInfo
props = info.Properties
For Each p As PropertyInfo In props
If p.Name = name Then
[code] Dim o As New Test
If o.HasProperty(“MyProperty”) Then
Thanks Markus, Ken, and Paul.
Yes, introspection is a very powerful tool and the code Paul presented is very helpful.
Our Java brethren have the functionality and was curious if this feature existed in Xojo. As Paul shows, its easy to build.