Introduction
A number of methods are available to determine the type of an object, each with its own specific characteristics. Before using them, you should know how to use them. I took me some time to find the last method (GetType) and to use it the way that is described in this article.
I hope it is of some use for you.
Before we start
The following abbreviations are used in this article:
var | Variable declared within a class, form, etcetera (variable) |
mvr | Variable declared within a method (method variable) |
par | Parameter |
mtd | Subroutines and functions (methods) |
enm | Enum |
enc | Enum constant |
usr | Declaration of a Usercontrol |
frm | Declaration of a Form |
The use of abbreviations seems to be an outdated method in the time of intellisense. But I am an old-fashioned guy and it still offers many advantages for me:
- You immediately recognize your own declarations, object definitions, subroutines, functions and so on.
- The scope of a variable is immediately shown.
- Some mistakes shows themselves in a natural way.
- You have to come up with fewer names. And believe me, coming up with good, meaningful, clear names is not an easy job at all.
An example of using the abbreviations (regardless of whether the code below makes sense or not):
Private Enum enmAction As Byte
encNo = 0
encYes = 1
End Enum
Private varText As String = "Test"
Private Function mtdTextToCapitals( _
ByVal parText As String, _
ByVal parAction As enmAction) As String
Dim mvrText As String = Trim(parText)
If parAction = enmAction.encYes Then
mvrText = mvrText.ToUpper
End If
Return mvrText
End Function
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim mvrText as String = mtdTextToCapitals(varText, enmAction.encYes)
End Sub
Lets start
In this article the next two UserControls are used:
Public Class usrX
Inherits Label
End Class
Public Class usrY
Inherits Label
End Class
And, for exemple in a form, two instances are made:
Private varX as new usrX
varX.Name = "NameX"
Private varY as new usrY
varY.Name = "NameY"
Name
Name
identifies an instance of an object by its name (string).
Example:
If varX.Name = "NameX" Then
End If
Characteristics:
- Inheritance is not taken into account.
- Fast, 10,000,000 checks takes about 0.25 seconds on my computer.
- No type checking, a typo in the name of the object is not detected by the compiler.
- Available in Forms and (User)Controls.
TypeName
TypeName
identifies an object by its typename (string).
Example:
If TypeName(varX) = "usrX" Then
End If
Characteristics:
- Inheritance is not taken into account.
- Quite slow, 10,000,000 checks takes about 2 seconds on my computer.
- No type checking, a typo in the name of the object is not detected by the compiler.
- Available in Forms, (User)Controls and Classes.
TypeOf
TypeOf
identifies an object by its typename.
Example:
If TypeOf varX Is usrX Then
End If
Characteristics:
- Inheritance is taken into account.
- Very fast, 10,000,000 checks takes about 0.04 seconds on my computer.
- Type checking, a typo in the name of the object will be detected by the compiler.
- Available in Forms, (User)Controls and Classes.
Example:
Dim mvrObject As Object = varX
If TypeOf mvrObject Is Label Then
Debug.WriteLine("Label")
End If
If TypeOf mvrObject Is usrX Then
Debug.WriteLine("usrX")
End If
If TypeOf mvrObject Is usrY Then
Debug.WriteLine("usrY")
End If
The result is:
Label
usrX
usrX
inherits the Label
, and so TypeOf
returns True for usrX
but also for Label
.
GetType
GetType
identifies an object by its typename.
Example:
If mvrObject.GetType Is GetType(usrX) Then
End If
Characteristics:
- Inheritance is not taken into account.
- Very fast, 10,000,000 checks takes about 0.045 seconds on my computer.
- Type checking, a typo in the name of the object will be detected by the compiler.
- Available in Forms, (User)Controls and Classes.
Example:
Dim mvrObject As Object = varX
If mvrObject.GetType Is GetType(Label) Then
Debug.WriteLine("Label")
End If
If mvrObject.GetType Is GetType(usrX) Then
Debug.WriteLine("usrX")
End If
If mvrObject.GetType Is GetType(usrY) Then
Debug.WriteLine("usrY")
End If
Despite the inheritance, the result is only:
usrX