Bookshelf Home | Contents | Index | PDF |
Siebel VB Language Reference > VB Language Reference > Dim StatementThis standard VB statement declares variables for use in a Basic program. SyntaxDim [Shared] variableName [As[ New] type] [, variableName [As[ New] type]] ... ReturnsUsageDim is a declaration statement. It is an abbreviation for Declare in Memory; however, you must use the short form. VariableName must begin with a letter and contain only letters, numbers, and underscores. A name can also be delimited by brackets, and any character can be used inside the brackets, except for other brackets.
If the As clause is not used, the type of the variable can be specified by using a type character as a suffix to variableName. The two different type-specification methods can be intermixed in a single Dim statement (although not on the same variable). Basic is a strongly typed language: variables must be given a data type or they are assigned the data type variant. The available data types are:
For details on these variable types, read VB Data Types. NOTE: Good programming practice is to declare every variable. To force variables to be explicitly declared, use the Option Explicit statement (read Option Explicit Statement). Place procedure-level Dim statements at the beginning of the procedure. Variables can be shared across modules. A variable declared inside a procedure has scope local to that procedure. A variable declared outside a procedure has scope local to the module. If you declare a variable with the same name as a module variable, the module variable is not accessible. For details, read Global Statement. The Shared keyword is included for backward compatibility with older versions of Basic. It is not allowed in Dim statements inside a procedure. It has no effect. Regardless of which mechanism you use to declare a variable, you can choose to use or omit the type character when referring to the variable in the rest of your program. The type suffix is not considered part of the variable name. CAUTION: You can declare several variables on one line; however, unless you include the type for each variable, the type applies only to the last variable declared. ArraysThe available data types for arrays are numbers, strings, variants, and records. Arrays of arrays and objects are not supported. Array variables are declared by including a subscript list as part of the variableName. The syntax to use for variableName is:
If startSubscript is not specified, 0 is used as the default. Thus, the statement Both startSubscript and endSubscript are valid subscripts for the array. The maximum number of subscripts that can be specified in an array definition is 60. The maximum total size for an array is limited only by the amount of memory available. If no subscriptRange is specified for an array, the array is declared as a dynamic array. In this case, the ReDim statement must be used to specify the dimensions of the array before the array can be used. NumbersNumeric variables can be declared using the As clause and one of the following numeric types: currency, integer, long, single, double. Numeric variables can also be declared by including a type character as a suffix to the name. Numeric variables are initialized to 0. ObjectsObject variables are declared using an As clause and a typeName of a class. Object variables can be set to refer to an object, and then used to access members and methods of the object using dot notation.
An object can be declared as New for some classes. For example:
In such instances, a Set statement is not required to create the object variable; a new object is allocated when the variable is used. NOTE: The New operator cannot be used with the Basic Object class. RecordsRecord variables are declared by using an As clause and a typeName that has been defined previously using the Type statement. The syntax to use is: Records are made up of a collection of data elements called fields. These fields can be of any numeric, string, variant, or previously defined record type. For details on accessing fields within a record, read Type Statement. StringsSiebel VB supports two types of strings: fixed-length and dynamic. Fixed-length strings are declared with a specific length (between 1 and 32767) and cannot be changed later. Use the following syntax to declare a fixed-length string:
Dynamic strings have no declared length, and can vary in length from 0 to 32,767. The initial length for a dynamic string is 0. Use the following syntax to declare a dynamic string: When initialized, fixed-length strings are filled with zeros. Dynamic strings are initialized as zero-length strings. VariantsDeclare variables as variants when the type of the variable is not known at the start of, or might change during, the procedure. For example, a variant is useful for holding input from a user when valid input can be either text or numbers. Use the following syntax to declare a variant: Variant variables are initialized to vartype Empty. ExampleThis example shows a Dim statement for each of the possible data types. ' Must define a record type before you can declare a record Sub Button_Click See AlsoGlobal Statement |
Siebel VB Language Reference |