Does a HasA function exist?

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

Class MyCar MySpeed as Integer End Class

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”
End If

If MyCarClass HasA MySpeed then //Does a form of this exist?
msgbox “Yes, MyCarClass has a property called MySpeed”
End If
[/code]

I couldn’t seem to find this in the documentation and not sure if someone else has experienced this.

Thanks!

Isn’t that what introspection is about?

See http://documentation.xojo.com/index.php/Introspection

Especially PropertyInfo

http://documentation.xojo.com/index.php/PropertyInfo

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
Using Xojo.Introspection
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
Return True
End If
Next

Return False
End Function[/code]

Usage:

[code] Dim o As New Test

If o.HasProperty(“MyProperty”) Then
MsgBox(“Yes!”)
End If[/code]

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.

Thanks everyone!