Siebel VB Language Reference > Using Siebel VB > About Declaring Procedures and Variables >
This topic includes information about using the Declare Variable statement to declare a variable. For more information about this statement and the format and arguments you can use with it, see Declare Variable Statement.
It is recommended that you place procedure-level Declare Variable statements at the beginning of the procedure.
For information about explicitly declaring a variable, see Force Explicit Declaration Statement.
Determining Variable Scope
You can share a variable across modules. The following locations where you declare a variable determines the scope of the variable:
- Declare in a procedure. The variable is local to that procedure.
- Declare outside a procedure. The variable is local to the module.
If you declare a variable that has the same name as a module variable, then you cannot access the module variable. For more information, see Declare Global Variable Statement.
Specifying the Type When You Declare a Variable
When you declare a variable, you can specify one of the following types:
If you do not specify a data type, then Siebel VB assigns the variant data type to this variable.
If you do not include the As clause, then you can specify the type argument. To specify this argument, you use a type character as a suffix of the variableName argument. You can use both type specification techniques in a single Declare Variable statement. You cannot use them simultaneously on the same variable.
You can omit the type character when you reference the variable in your code. The type suffix is not part of the variable name.
For more information, see About Data Types.
Declaring an Array Variable
The following data types are available for an array:
You cannot use the Declare Variable statement to declare an array of arrays or an array of objects.
To declare an array variable, you include a subscript list as part of the variableName argument. You can use one of the following formats:
, ...]) As typeName
, ... ])
Table 11 describes the startSubscript and endSubscript arguments.
Table 11. Start Subscript and End Subscript Arguments
The index number of the first array element, followed by the following keyword:
The index number of the last element of the array.
Specifying Arguments When Declaring an Array
The startSubscript argument is optional. If you do not specify it, then Siebel VB uses zero as the default value. For example, the following statement creates an array named counter that includes elements 0 through 25, for a total of 26 elements. To change the default value, you can use the Set Array Lower Boundary statement:
Dim counter (25) as Integer
The values in the startSubscript argument and the endSubscript argument are valid subscripts for the array.
Size Limits of an Array
You can specify no more than 60 arrays in a parent array. The maximum total number of elements cannot exceed 65,536. For example, the following code is valid because 60 multiplied by 1092 is 65,520, which is less than 65,536:
Dim count(1 To 60, 1 To 1092)
The following code is not valid because 60 multiplied by 1093 is 65,580, which is more than 65,536:
Dim count(1 To 60, 1 To 1093)
Each subscript declares one array in the parent array. If you do not specify the subscriptRange argument, then Siebel VB declares the array as a dynamic array. In this situation, you must use the Declare Array method to specify the dimensions of the array before you can use it.
Declaring a Number Variable
To declare a numeric variable, you can use the As clause and one of the following numeric types:
To declare a numeric variable, you can also include a type character as a suffix to the variable name. Siebel VB sets a numeric variable to 0.
Declaring a Record Variable
To declare a record variable, you use the As clause and specify a value in the typeName argument. To define this type, you must use the Type statement before you can specify it in the typeName argument. You use the following format:
A record includes a collection of data elements that are fields. Each field can be a numeric, string, variant, or previously defined record type. For more information on accessing fields in a record, see Create Function Method.
Declaring a String Variable
Siebel VB supports the following types of strings:
Declaring a Variant Variable
You declare a variable as a variant in the following situations:
- If the type of the variable is not known.
- If the variable type might change when the code runs. For example, a variant is useful for holding input from a user when valid input can be text or numbers.
To declare a variant variable, you use one of the following formats:
Siebel VB initializes a variant variable to the Empty variant type.
For more information, see Variants.
Declaring an Object Variable
To declare an object variable, you use the As clause and specify a class in the typeName argument. An object variable can reference an object. To access members and methods of this object, it can use dot notation. For example:
For some classes, you can declare an object as New. For example:
As New className
In this situation, a Set statement is not required. Siebel VB allocates a new object when it uses this variable.
NOTE: You cannot use the New operator with the Basic Object class.
Caution About Declaring Multiple Variables on One Line
CAUTION: Although you can declare multiple variables on one line, unless you include the type for each variable, Siebel VB applies the type of the last variable to all the variables that you declare on this line.
For example, the following code declares all the variables as strings:
Dim Acct, CustName, Addr As Stringcauses only
Addr to be declared as type string; the other variables are implicitly declared as type variant. On the other hand,
Dim Acct As String, CustName As String, Addr As String
Shared Keyword Allows Backward Compatibility
To allow backward compatibility with older versions of Visual Basic, Siebel VB includes the Shared keyword. You cannot use it in a Declare Variable statement in a procedure. If you use the Shared keyword in a Declare Variable statement in a procedure, then it has no effect.