Visual Basic.NET 2008 > Using Variables and Data Types

<< Previous Page Next Page >>

Examining Variable Types - Variables in Visual Basic 2008

Besides setting the types of variables and the functions for converting between types, Visual Basic provides the GetType method, which returns a string with the variable's type (Int32, Decimal, and so on). Any variable exposes these methods automatically, and you can call them like this:

Dim var As Double
Debug.WriteLine "The variable's type is " & var.GetType.ToString

There's also a GetType operator, which accepts as an argument a type and returns a Type object for the specific data type. The GetType method and GetType operator are used mostly in If structures, like the following one:

If var.GetType() Is GetType(Double) Then
{ code to handle a Double value}
End If

Notice that the code doesn't reference data type names directly. Instead, it uses the value returned by the GetType operator to retrieve the type of the class System.Double and then compares this value to the variable's type with the Is (or the IsNot) keyword.

<< Previous Page Next Page >>

Table of Contents

     
 
W3computing.com Copyright 2011 © All Rights Reserved
 
  Home | Useful links | Contact us