Untyped variables can’t be resolved at compile time; these variables are said to be late-bound. An expression such as the following can’t be resolved at compile time because the compiler has no way of knowing whether the object retrieved from the ListBox control is of the Customer type (or any other type that exposes the LastName property):
Code language: CSS (css)
The preceding statement will compile and execute fine if the fourth item on the ListBox control is of the Customer type or any other type that provides a LastName property. If not, it will compile all right, but a runtime exception will be thrown.Moreover, you won’t see any members of interest in the IntelliSense box, because the editor doesn’t know the exact type of the object retrieved from the ListBox control.
If you cast the object to a specific type, the compiler won’t let you reference a nonexisting member, therefore eliminating the chances of runtime exceptions. The last expression in the following code segment is said to be early-bound because the compiler knows its type and won’t compile a statement that references nonexisting members:
Code language: PHP (php)
Dim currentCustomer As Customer currentCustomer = CType(ListBox1.Items(3), Customer) Debug.WriteLine currentCustomer.LastName
Casting an object to the desired type won’t help you, unless you know that the object is of the same type or can be cast to the desired type. Make your code as robust as it can be by using the TryCast() function to make sure that the conversion succeeded before attempting to use the currentCustomer object in your code.
Discovering a Variable’s Type
Sometimes we need to figure out the type of a variable in our code. Even if you declare explicitly all the variables in your code, you might have to discover a specific variable’s type at runtime. The Form object exposes the ActiveControl property, which is the control that has the focus. The ActiveControl property returns a Control object, and you will have to find out its exact type (whether it’s a TextBox, a ComboBox, or a Button, for example) from within your code.
All classes, including custom ones, expose the GetType() function, which returns the type of the corresponding object. The GetType() function’s return value isn’t a string; it is an object that exposes a large number of properties. You can call the IsEnum and IsClass properties to find out whether it’s been implemented as an enumeration or as a class, as well as the Name property to retrieve the variable’s type name.
Consider an event handler that handles the same event for multiple controls on a form. The control that raised the event is passed to the event handler through the sender argument, and you can determine the type of the control that raised the event by using a statement such as the following:
If sender.GetType Is GetType(System.Windows.Forms.Button) Then ' process a button control End If
You can also retrieve the type’s name with the TypeName() function, which returns a string:
If TypeName(newContact).ToUpper="CONTACT" Then
Because the TypeName() function returns a string, you don’t have to use the Is operator, but it’s a good idea to convert this value to uppercase before attempting any comparisons.
Notice that you can’t use the equals operator to compare types. To compare an object’s type to another type, you must use the Is and IsNot keywords, as shown in the preceding example.
By now you should have a good understanding of developing with objects. In the following section, you’re going to learn about a powerful concept in OOP, namely how to write new classes that inherit the functionality of existing ones.