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 provides two means of determining which type of object is stored in an object variable: the TypeName function and the TypeOf...Is operator.
TypeName and TypeOf…Is
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:
Dim Ctrl As Control = New TextBox
MsgBox(TypeName(Ctrl))
The TypeOf...Is 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 TypeOf...Is 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 TypeOf...Is 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 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 Object. When passed an Integer and evaluated with Object, the TypeOf...Is operator returns True. The following example reports that the parameter InParam is both an Object and an Integer:
Sub CheckType(ByVal InParam As Object)
' 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 TypeOf...Is 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
Create a new Windows Application project and add a Button control, a CheckBox control, and a RadioButton control to the form.
From the button on your form, call the TestObject procedure.
Add the following code to your form:
Sub ShowType(ByVal Ctrl As Object) 'Use the TypeName function to display the class name as text. MsgBox(TypeName(Ctrl)) '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.") Else 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. ShowType(Me.Button1) ShowType(Me.CheckBox1) ShowType(Me.RadioButton1) End Sub
See Also
Concepts
Calling a Property or Method Using a String Name
Reference
TypeName Function (Visual Basic)
If...Then...Else Statement (Visual Basic)
String Data Type (Visual Basic)
Integer Data Type (Visual Basic)