Pages

Monday, July 4, 2011

Determine the type of data in a variable using the VarType function (VBScript 5.0)

In VBSscript, all variables are variant types. However, even though the browser doesn't care what you put in the container, your application might. You can easily determine what type of data you've placed in a variable using the VarType() function.

The VarType function takes a variable, evaluates its contents, and returns an integer value that identifies the variable's subtype. It's the perfect tool when you need to know precisely what's stored in a variable. For example, the IsNumeric function will tell you that num1 and num2 in the following code are numbers:

Dim num1: num1 = 6
Dim num2: num2 = 6e6

Response.write IsNumeric(num1) & "<br>"
Response.write IsNumeric(num2) & "<br>"

But, only the VarType() function can tell you that num1 is an Integer and num2 is a Double. When the browser executes the next two lines of code, it will display 2 for the VarType of num1 and 5 for the VarType of num2:

Response.write VarType(num1) & "<br>"
Response.write VarType(num2) & "<br>"

The table below shows what those (and other numbers the function might return) mean:


CONSTANT VALUE DESCRIPTION
=========================================
VBEmpty       0  Uninitialized
VBNull        1  Contains no valid data
VBInteger     2  Integer subtype
VBLong        3  Long subtype
VBSingle      4  Single subtype
VBDouble      5  Double subtype
VBCurrency    6  Currency subtype
VBDate        7  Date subtype
VBString      8  String subtype
VBObject      9  Object
VBError      10  Error subtype
VBBoolean    11  Boolean subtype
VBVariant    12  Variant (only for arrays) 
VBDataObject 13  Data access object
VBDecimal    14  Decimal subtype
VBByte       17  Byte subtype
VBArray    8192  Array


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.