Siebel VB Language Reference > VB Language Reference >

Dim Statement


This standard VB statement declares variables for use in a Basic program.

Syntax

Dim [Shared] variableName [As[ New] type] [, variableName [As[ New] type]] ...

Placeholder
Description

variableName

The name of the variable to declare

type

The data type of the variable

Returns

Not applicable

Usage

Dim 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.

Dim my_1st_variable As String

Dim [one long and strange! variable name] As String

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:

  • Array
  • Double (double-precision floating-point number)
  • Integer
  • Long (double-precision integer)
  • Object
  • Record
  • Single (single-precision floating-point number)
  • String
  • Variant

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.

For example,
Dim Acct, CustName, Addr As String
causes 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
declares the variables as type string.

Arrays

The 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:

Dim variable([[startSubcript To] endSubscript, ...]) As typeName

or

Dim variable_with_suffix([[startSubcript To] endSubscript, ... ])

Argument
Description

startSubscript

[Optional] the index number of the first array element, followed by the keyword To

endSubscript

The index number of the last element of the array

If startSubscript is not specified, 0 is used as the default. Thus, the statement
Dim counter (25) as Integer
creates an array named counter that has 26 elements (0 through 25). To change the default, use the Option Base 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.

Numbers

Numeric 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.

Objects

Object 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.

Dim COMObject As Object
Set
COMObject = CreateObject("spoly.cpoly")
COMObject.reset

An object can be declared as New for some classes. For example:

Dim variableName As New className
variableName.methodName

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.

Records

Record 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:

Dim variableName As typeName

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.

Strings

Siebel 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:

Dim variableName As String * length

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:

Dim variableName$

or

Dim variableName As String

When initialized, fixed-length strings are filled with zeros. Dynamic strings are initialized as zero-length strings.

Variants

Declare 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:

Dim variableName

or

Dim variableName As Variant

Variant variables are initialized to vartype Empty.

Example

This example shows a Dim statement for each of the possible data types.

' Must define a record type before you can declare a record
' variable
   Type Testrecord
      Custno As Integer
      Custname As String
   End Type

Sub Button_Click
   Dim counter As Integer
   Dim fixedstring As String * 25
   Dim varstring As String
   Dim myrecord As Testrecord
   Dim ole2var As Object
   Dim F(1 to 10), A()
   '...(code here)...
End Sub

See Also

Global Statement
Option Base Statement
ReDim Statement
Service_InvokeMethod Event
Static Statement
Type Statement

Siebel VB Language Reference