IsA

From Xojo Documentation

Operator

Used to determine the class of a particular object reference.

Usage

result = object IsA object class

Part Type Description
result Boolean Any container expecting a boolean value.
object Object Any object name.
object class Object class Any object class or class interface.

Notes

If object is of type class, then IsA returns True; if not, it returns False. If object is Nil, then IsA returns False. The IsA operator returns True for the object's own class as well as the super class from which that class was derived, all the way to the Object class. IsA will report that all classes ultimately derive from Object.

Remember that object must always be a class instance, not just a variable with the class of the type. For example, this code always returns False because d is not an instance:

Var d As Dictionary

' Returns False because d is not an instance
If d IsA Dictionary Then
...
Else
...
End If

Instead you create an instance like this:

Var d As New Dictionary

' Returns True because d an instance of Dictionary
If d IsA Dictionary Then
...
Else
...
End If

Superclasses

The IsA operator returns True for the object's own class as well as the super class from which that class was derived, all the way to the Object class. IsA will report that all classes ultimately derive from Object.

For example, suppose you create a subclass of TextField called SecureTextField, that disables the Cut and Copy commands in the Edit menu. If TextField1 is an instance of SecureTextField, the tests:

TextField1 IsA TextField

and

TextField1 IsA SecureTextField

both return True.

Casting

You use the IsA operator to cast objects. With the IsA operator, you test whether an object is of a specific subclass and, if it is, cast it as that type to do something specific with it.

Here is an example that uses a For loop to cycle through all the controls in a window to test whether each control is a TextField. If it is, it casts the control, gets its name and the value of its Text property, and assigns the contents of the TextField to the field in a database table named fieldname.

For i As Integer = 0 To Self.ControlCount - 1
// Check if the control is a TextField
If Self.Control(i) IsA TextField Then
// Cast the control to a TextField in order to access the Text property
TextField(Self.Control(i)).Value = "Found You!"
End If
Next

As you can see, the code is generic since it uses no references to specific TextFields or databases.

A second example of casting uses the Window function to get the frontmost window and then using IsA to determine which what kind of window it is (i.e., Window1, Window2) and then casts it as that type to access something specific about the window

If Window(0) IsA Window1 then
Window1(Window(0)).Pushbutton1.Enabled = True
End if

Select...Case

The IsA operator works in Select...Case statements like this (in the Action event of a PushButton):

Select Case Me
Case IsA PushButton
MessageBox("I'm a PushButton.")
Case IsA TextField
MessageBox("Nope!")
End Select

Class Interfaces

You also use the IsA operator in connection with a class interface. If a custom class implements a particular class interface, then IsA return True. For example, if you create a class interface, myClassInterface, and custom control classes subclassed from the Label, TextFields, TextAreas, and Canvas classes all implement myClassInterface, then the IsA operator will return True for each such test.

In this way, you can write generic code that examines all controls in a window to determine whether each control implements a particular class interface. If it does, then you can execute any methods of the class interface.

For example, if you wrote a class interface called FontInterfaceManger that updates the font displayed by a control using a method called UpdateTheFont, you use code such as this to call the method only for the controls in a window that implement the FontInterfaceManager class interface. This following example, which can be the Action event handler of a PushButton in a window, examines all the controls in the window and calls the UpdateTheFont method for those controls that implement the class interface FontInterfaceManager.

For i As Integer = 0 To Self.ControlCount - 1
If Self.Control(i) IsA FontInterfaceManager Then
FontInterfaceManager(Self.Control(i)).UpdateTheFont
End If
Next

Sample Code

This example uses the IsA operator to determine if the variable p is a PushButton object. If it is, the caption property is assigned "OK".

If p IsA PushButton Then
PushButton(p).Caption="OK"
End if

See Also

Select Case statement; Operator precedence