Variables, Arrays, Constants and  Data Types

<< Click to Display Table of Contents >>

Navigation:  Reference (Scripting) > VBScript Basics >

Variables, Arrays, Constants and  Data Types

You often need to store values temporarily when performing calculations with VBScript. For example, you might want to calculate several values, compare them, and perform different operations on them, depending on the result of the comparison.

 

Variables

 

Variable names follow the standard rules for naming anything in VBScript. A variable name:

Must begin with an alphabetic character.

Cannot contain an embedded period.

Must not exceed 255 characters.

Must be unique in the scope in which it is declared.

 

' Examples

ApplesSold = 10 ' The value 10 is passed to the variable.

ApplesSold = ApplesSold + 1 ' The variable is incremented.

 

Arrays

Arrays allow you to refer to a series of variables by the same name and to use a number (an index) to tell them apart. This helps you create smaller and simpler code in many situations, because you can set up loops that deal efficiently with any number of cases by using the index number.

 

' Example

Dim A(10)

 

A(0) = 256

A(1) = 324

A(2) = 100

' ...

A(10) = 55
 

 

LBound Function

Returns the smallest available subscript for the indicated dimension of an array.

 

Syntax

 

LBound (arrayname [,dimension ] )

 

Part

Description

arrayname

Name of the array variable; follows standard variable naming conventions.

dimension

Optional. Whole number indicating which dimension's lower bound is returned. Use 1 for the first dimension, 2 for the second, and so on. If dimension is omitted, 1 is assumed.

 

 

UBound Function

Returns the largest available subscript for the indicated dimension of an array.

 

Syntax

 

UBound (arrayname [,dimension ] )

 

Part

Description

arrayname

Name of the array variable; follows standard variable naming conventions.

dimension

Optional. Whole number indicating which dimension's lower bound is returned. Use 1 for the first dimension, 2 for the second, and so on. If dimension is omitted, 1 is assumed.

 

 

' Example

Dim A(100,3,4)

UBound(A,1) ' returns 100

UBound(A,2) ' returns 3

UBound(A,3) ' returns 4

 

 

Constants

 

A Const statement can represent a mathematical or date/time quantity:

 

' Example

Const conPi = 3.14159265358979

 

 

Data Types

 

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

 

Variant Subtypes

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. 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 the Variant behaves in a way that is most appropriate for the data it contains.

 

The following table shows subtypes of data that a Variant can contain.

 

Subtype

Description

Empty

Variant is uninitialized. Value is 0 for numeric variables or a zero-length string ("") for string variables.

Null

Variant intentionally contains no valid data.

Boolean

Contains either True or False.

Byte

Contains integer in the range 0 to 255.

Integer

Contains integer in the range -32,768 to 32,767.

Currency

-922,337,203,685,477.5808 to 922,337,203,685,477.5807.

Long

Contains integer in the range -2,147,483,648 to 2,147,483,647.

Single

Contains a single-precision, floating-point number in the range -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values.

Double

Contains a double-precision, floating-point number in the range -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values.

Date (Time)

Contains a number that represents a date between January 1, 100 to December 31, 9999.

String

Contains a variable-length string that can be up to approximately 2 billion characters in length.

Object

Contains an object.

Error

Contains an error number.