Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / .NET

TypeName, TypeOf and GetType in VisualBasic

4.71/5 (3 votes)
17 Aug 2024Public Domain2 min read 5.2K  
A number of methods are available to determine the type of an object, each with its own specific characteristics.

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):

VB.NET
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:

VB.NET
Public Class usrX
  Inherits Label
End Class

Public Class usrY
  Inherits Label
End Class

And, for exemple in a form, two instances are made:

VB.NET
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:

VB.NET
If varX.Name = "NameX" Then
  'Action here
End If

Characteristics:

  1. Inheritance is not taken into account.
  2. Fast, 10,000,000 checks takes about 0.25 seconds on my computer.
  3. No type checking, a typo in the name of the object is not detected by the compiler.
  4. Available in Forms and (User)Controls.

TypeName

TypeName identifies an object by its typename (string).

Example:

VB.NET
If TypeName(varX) = "usrX" Then
  'Action here
End If

Characteristics:

  1. Inheritance is not taken into account.
  2. Quite slow, 10,000,000 checks takes about 2 seconds on my computer.
  3. No type checking, a typo in the name of the object is not detected by the compiler.
  4. Available in Forms, (User)Controls and Classes.

TypeOf

TypeOf identifies an object by its typename.

Example:

VB.NET
If TypeOf varX Is usrX Then
  'Action here
End If

Characteristics:

  1. Inheritance is taken into account.
  2. Very fast, 10,000,000 checks takes about 0.04 seconds on my computer.
  3. Type checking, a typo in the name of the object will be detected by the compiler.
  4. Available in Forms, (User)Controls and Classes.

Example:

VB.NET
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:

VB.NET
If mvrObject.GetType Is GetType(usrX) Then
  'Action here
End If

Characteristics:

  1. Inheritance is not taken into account.
  2. Very fast, 10,000,000 checks takes about 0.045 seconds on my computer.
  3. Type checking, a typo in the name of the object will be detected by the compiler.
  4. Available in Forms, (User)Controls and Classes.

Example:

VB.NET
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

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication