Variables. Subtypes


© Maxim Karetnikov

VBScript has only one data type and it's called a Variant. A Variant is a special kind of data type that can contain different kinds of information, depending on how it's used. Because Variant is the only data type in VBScript, it's also the data type returned by all functions in VBScript.

At its simplest, a Variant can contain either numeric or string information. A Variant behaves as a number when you're using it in a numeric context and as a string when you use it in a string context. That is, if you're working with data that looks like numbers, VBScript assumes that it is numbers and does the thing that is most appropriate for numbers. Of course, you can always make numbers behave as strings, by enclosing them in quotation marks. Similarly, if you're working with data that can only be string data, VBScript treats it as string data.

Beyond the simple numeric or string classifications, a Variant can make further distinctions about the specific nature of numeric information. For example, you can have numeric information that represents a date or a time. When used with other date or time data, the result is always expressed as a date or a time. Of course, you can also have a rich variety of numeric information ranging in size from Boolean values to huge floating-point numbers. These different categories of information that can be contained in a Variant are called subtypes. Most of the time, you can just put the kind of data you want in a Variant and it behaves in a way that is most appropriate for the data it contains.

For explicit control of data subtypes, VBScript provides the VarType function and data conversion functions. The syntax of VarType function is as follows:

retValue=VarType(varname)

The varname argument can be any variable. Returned value (retValue) indicates the subtype of a variable. VBScript also provides the explicit conversion of variables of one subtype to another. For instance, to convert a variable to a string the function CStr is used:

StringVariable=CStr(anyVariable)

The basic data subtypes along with value returned by VarType function, and corresponding data conversion function are given in table below.

Go To Page: 1 2


The copyright of the article Variables. Subtypes in VB Script is owned by . Permission to republish Variables. Subtypes in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo





Subtype Description VarType() Data conversion function
Boolean Contains either True or False. 11 CBool
Byte Contains integer in the range 0 to 255.