NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Dim Statement

Declares variables and allocates storage space.

Dim [WithEvents] varname[([subscripts])] [As [New] type][= initexpr]

The Dim statement syntax has these parts:

Part Description
WithEvents Optional. Keyword that specifies that varname is an object variable used to respond to events raised by the instance which is assigned to the variable. You can declare as many individual variables as you like using WithEvents, but you can't create arrays with WithEvents.
varname Required. Name of the variable; follows standard variable naming conventions.
Initial arraysize Optional. Specifies the initial size of each dimention of the array.Up to 60 multiple dimensions may be declared.
New Optional. When New
type Optional. Data type of the variable; may be Byte, Boolean, Char, Short, Integer, Long, Single, Double, Decimal, Date, String, Object, a user-defined type, or an object type. Use a separate As type clause for each variable you declare.
initexpr Optional. Any expression that provides the initial value to assign to the variable when it’s declared. A variable cannot be initialized using this syntax if an “As New” clause is used.

Remarks

Variables declared with Dim at the module level are available to all procedures within the module. At the procedure level, variables are available only within the procedure.

Use the Dim statement at module or procedure level to declare the data type of a variable. For example, the following statement declares a variable as an Integer and initializes the value to 10

Dim NumberOfEmployees As Integer = 10

Also use a Dim statement to declare the object type of a variable. The following declares a variable for a new instance of a worksheet.

Dim X As New Worksheet

If the New keyword is not used when declaring an object variable, the variable that refers to the object must be assigned an existing object before it can be used. Until it is assigned an object, the declared object variable has the special value Nothing, which indicates that it doesn't refer to an instance of an object.

You can initialize the values of an array by providing a list of initialization values with curly braces. The following example creates an array with four elements and initializes those elements:

Dim A(4) As Integer = {0, 1, 2, 3}

The following example creates a fixed-rank array and initializes 3 elements:

Dim A(3) As Integer = {10, 20, 30}

For multi-dimensional arrays, each separate dimension is included in curly braces. The elements are listed in row-major order. For example:

 ‘ Initialize array elements to their row index * their column index
Dim a(2, 3) As Integer = {{1*3, 1*4, 1*5},{2*3 ,2*4, 2*5}}

If you don't specify a data type or object type, the variable is Object by default.

When no initialization value is specified, a numeric variable is initialized to 0, a string is initialized to a zero-length string (""). Object variables are initialized to Nothing. Each element of a structure variable is initialized as if it were a separate variable.

Note   When you use the Dim statement in a procedure, you generally put the Dim statement at the beginning of the procedure.

See Also

Example

Array Function | Data Type Summary | Private Statement | Public Statement | ReDim Statement