Operator

IsA


Description

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 DesktopTextField

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 DesktopTextField. If it is, it casts the control, gets its name and the value of its Text property, and assigns the contents of the DesktopTextField to the field in a database table named fieldname.

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

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

A second example of casting uses the DesktopWindow 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)).Button1.Enabled = True
End if

Select...case

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

Select Case Me
Case IsA DesktopButton
  MessageBox("I'm a Button.")
Case IsA DesktopTextField
  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 Pressed event handler of a DesktopButton 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.ControlAt(i) IsA FontInterfaceManager Then
    FontInterfaceManager(Self.ControlAt(i)).UpdateTheFont
  End If
Next

Sample code

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

If b IsA DesktopButton Then
DesktopButton(b).Caption="OK"
End if

Compatibility

All project types on all supported operating systems.

See also

Select Case statement; Operator precedence