Operator

# IsA

<div class="rst-class">

forsearch

</div>

Operator

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Used to determine the class of a particular object reference.

## Usage

``` xojo
result = object IsA object class
```

| Part         | Type                                              | Description                                                         |
|--------------|---------------------------------------------------|---------------------------------------------------------------------|
| result       | `Boolean</api/data_types/boolean>`                | Any container expecting a `boolean</api/data_types/boolean>` value. |
| object       | `Object</api/data_types/additional_types/object>` | Any object name.                                                    |
| object class | Object class                                      | Any object class or class interface.                                |

## Notes

If object is of type class, then <span class="title-ref">IsA</span> returns `True</api/language/true>`; if not, it returns `False</api/language/false>`. If object is `Nil</api/language/nil>`, then <span class="title-ref">IsA</span> returns `False</api/language/false>`. The <span class="title-ref">IsA</span> operator returns `True</api/language/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. <span class="title-ref">IsA</span> 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</api/language/false>` because d is not an instance:

``` xojo
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:

``` xojo
Var d As New Dictionary

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

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Superclasses

The <span class="title-ref">IsA</span> operator returns `True</api/language/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</api/data_types/additional_types/object>` class. <span class="title-ref">IsA</span> will report that all classes ultimately derive from `Object</api/data_types/additional_types/object>`.

For example, suppose you create a subclass of `TextField</api/user_interface/desktop/desktoptextfield>` called SecureTextField, that disables the Cut and Copy commands in the Edit menu. If TextField1 is an instance of SecureTextField, the tests:

``` xojo
TextField1 IsA DesktopTextField
```

and

``` xojo
TextField1 IsA SecureTextField
```

both return `True</api/language/true>`.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Casting

The <span class="title-ref">IsA</span> operator is often used when casting objects to a specific type. With the <span class="title-ref">IsA</span> 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</api/language/loops/for...next>` loop to cycle through all the controls in a window to test whether each control is a `DesktopTextField</api/user_interface/desktop/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</api/user_interface/desktop/desktoptextfield>` to the field in a database table named *fieldname*.

``` xojo
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</api/user_interface/desktop/desktoptextfield>` or databases.

A second example of casting uses the `DesktopWindow</api/user_interface/desktop/desktopwindow>` function to get the frontmost window and then using <span class="title-ref">IsA</span> 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:

``` xojo
If Window(0) IsA Window1 then
  Window1(Window(0)).Button1.Enabled = True
End if
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Select...case

The <span class="title-ref">IsA</span> operator works in Select...Case statements like this (in the Pressed event of a `DesktopButton</api/user_interface/desktop/desktopbutton>`):

``` xojo
Select Case Me
Case IsA DesktopButton
  MessageBox("I'm a Button.")
Case IsA DesktopTextField
  MessageBox("Nope!")
End Select
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Class interfaces

You also use the <span class="title-ref">IsA</span> operator in connection with a class interface. If a custom class implements a particular class interface, then <span class="title-ref">IsA</span> returns `True</api/language/true>`. For example, if you create a class interface, myClassInterface, and custom control classes subclassed from the `Label</api/user_interface/desktop/desktoplabel>`, `TextFields</api/user_interface/desktop/desktoptextfield>`, `TextAreas</api/user_interface/desktop/desktoptextarea>`, and `Canvas</api/user_interface/desktop/desktopcanvas>` classes all implement myClassInterface, then the <span class="title-ref">IsA</span> operator will return `True</api/language/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.

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

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Aggregated class interfaces

When a class interface aggregates other class interfaces, <span class="title-ref">IsA</span> returns `True</api/language/true>` not only for the interface the class directly implements, but also for each interface that is aggregated by it.

For example, consider these interfaces:

``` xojo
Interface Readable
  Function Read() As String
End Interface

Interface Writeable
  Sub Write(value As String)
End Interface

Interface ReadableAndWriteable
  Aggregates Readable, Writeable
End Interface
```

If a class implements <span class="title-ref">ReadableAndWriteable</span>, then <span class="title-ref">IsA</span> returns `True</api/language/true>` for <span class="title-ref">ReadableAndWriteable</span>, <span class="title-ref">Readable</span>, and \`Writeable\`:

``` xojo
Var obj As New MyReadableAndWriteableClass

obj IsA ReadableAndWriteable  ' True
obj IsA Readable              ' True
obj IsA Writeable             ' True
```

This means you can pass an object that implements an aggregated interface to any method that expects one of its constituent interfaces.

## Sample code

This example uses the <span class="title-ref">IsA</span> operator to determine if the variable p is a `DesktopButton</api/user_interface/desktop/desktopbutton>` object. If it is, the caption property is assigned "OK".

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

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Select Case</api/language/select_case>` statement; `Operator precedence</api/language/operators/operator_precedence>`

</div>
