This documentation is archived and is not being maintained.

Determining Object Type

Generic object variables (that is, variables you declare as Object) can hold objects from any class. When using variables of type Object, you may need to take different actions based on the class of the object; for example, some objects might not support a particular property or method. Visual Basic .NET provides two means of determining which type of object is stored in an object variable: the TypeName function and the TypeOf...Is operator.

The TypeName function returns a string and is the best choice when you need to store or display the class name of an object, as shown in the following code fragment:


The TypeOfIs operator is the best choice for testing an object's type, because it is much faster than an equivalent string comparison using TypeName. The following code fragment uses TypeOfIs within an If...Then...Else statement:

If TypeOf Ctrl Is Button Then
   MsgBox("The control is a button.")
End If

A word of caution is due here. The TypeOfIs operator returns True if an object is of a specific type, or is derived from a specific type. Almost everything you do with Visual Basic .NET involves objects, which include some elements not normally thought of as objects, such as Strings and Integers. These objects are derived from and inherit methods from System.Object. When passed an Integer and evaluated with Object, the TypeOfIs operator returns True. The following example reports that the parameter InParam is both an Object and an Integer:

Sub CheckType(ByVal InParam)
' Both If statements evaluate to True when an
' Integer is passed to this procedure.
   If TypeOf InParam Is Object Then
      MsgBox("InParam is an Object")
   End If
   If TypeOf InParam Is Integer Then
      MsgBox("InParam is an Integer")
   End If
End Sub

The following example uses both TypeOfIs and TypeName to determine the type of object passed to it in the Ctrl argument. The TestObject procedure calls ShowType with three different kinds of controls.

To run the example

  1. Create a new Windows Forms project and add a Button control, a CheckBox control, and a RadioButton control to the form.
  2. From the button on your form, call the TestObject procedure.
  3. Add the following code to the Declarations section of your form:
    Sub ShowType(ByVal Ctrl As Object)
       'Use the TypeName function to display the class name as text.
       'Use the TypeOf function to determine the object's type.
       If TypeOf Ctrl Is Button Then
          MsgBox("The control is a button.")
       ElseIf TypeOf Ctrl Is CheckBox Then
          MsgBox("The control is a check box.")
          MsgBox("The object is some other type of control.")
       End If
    End Sub
    Protected Sub TestObject()
    'Test the ShowType procedure with three kinds of objects.
    End Sub

See Also

Calling a Property or Method Using a String Name | Performing Multiple Actions on an Object | Obtaining Class Information at Run Time | Object Data Type | TypeOf | TypeName Function | If...Then...Else Statements | String Data Type | Integer Data Type