Dim

Syntax

Dim [Shared] Var1 [(Size1 [, Size2] [, ...])] [, Var2 [(Size1 [, Size2] [, ...])]] [, ...]

Dim DialogRecord As DialogName

Dim DialogRecord As UserDialog

Remarks

Declares variables to be shared by more than one subroutine, defines array variables, or defines a dialog record. Dim statements that declare shared variables must be placed outside the macro's subroutines — usually before the main subroutine.

Syntax

Purpose

Dim Var(Size1 [, Size2]
[, ...])

Defines an array variable. For one-dimensional arrays, Size1 is the subscript of the last element in the array.

For multidimensional arrays, each Size argument is the subscript of the last element for the dimension of the array to be defined.

In WordBasic, the first element in an array is always numbered 0 (zero), so to define a one-dimensional array of seven elements — for example, the days of the week — Size1 would be 6. To define a two-dimensional array of 12 rows by 31 columns — for example, the days of the year organized by month — Size1 would be 11 and Size2 would be 30.

Dim Shared Var[(Size1
[, Size2] [, ...])]

Declares a string, numeric, or array variable to be shared
by more than one subroutine. The Dim statement must be placed outside any subroutine. Usually, shared variable declarations are placed at the start of a macro, before the main subroutine.


Syntax

Purpose

Dim DialogRecord As DialogName

Defines a dialog record variable for a Word dialog box. DialogName can be any WordBasic statement that corresponds to a Word command or dialog box. For example, FormatFont or FileOpen are valid as DialogName.

Dim DialogRecord As UserDialog

Defines a dialog record for a user-defined dialog box.


Examples

This example declares three variables to be shared by multiple subroutines and would typically be placed before the Sub MAIN instruction.


Dim Shared numDocs, docName$, savePath$

The following example stores the names of the days of the week in the array Days$ and displays the third element of the array, "Tuesday," in the status bar:


Dim Days$(6)
Days$(0) = "Sunday" : Days$(1) = "Monday" : Days$(2) = "Tuesday"
Days$(3) = "Wednesday" : Days$(4) = "Thursday"
Days$(5) = "Friday" : Days$(6) = "Saturday"
Print Days$(2)

The following example stores the names of the days of the week and their Spanish translations in the two-dimensional array Days$. The last instruction displays "Martes," the element in the third row of the second column of the array, in the status bar:


Dim Days$(6, 1)
Days$(0, 0) = "Sunday" : Days$(0, 1) = "Domingo" : Days$(1, 0) = "Monday"
Days$(1, 1) = "Lunes" : Days$(2, 0) = "Tuesday" : Days$(2, 1) = "Martes"
Days$(3, 0) = "Wednesday" : Days$(3, 1) = "Miércoles" : Days$(4, 0) = "Thursday"
Days$(4, 1) = "Jueves" : Days$(5, 0) = "Friday" : Days$(6, 1) = "Viernes"
Days$(5, 1) = "Saturday" : Days$(6, 0) = "Sábado"
Print Days$(2, 1)

The following example declares the variable num as shared, sets the variable to 6, and then calls the MyRoutine subroutine. The subroutine adds 10 to num, and the main subroutine displays the new value. Note that the instruction Dim Shared num must be placed outside both subroutines.


Dim Shared num                    'Declare num as shared
Sub MAIN
    num = 6                        'Set the value of num
    MyRoutine                    'Call the routine
    Print num                    'Display the new value of num
End Sub

Sub MyRoutine
    num = num + 10                'Increase the value of num
End Sub

In the following example, Dim is used to define the dialog record TOVrec in which the values of the View tab in the Options dialog box (Tools menu) are stored. This macro toggles the setting of the Paragraph Marks check box.


Dim TOVrec As ToolsOptionsView    'Define dialog record
GetCurValues TOVrec                 'Get the current state
If TOVrec.Paras = 1 Then            'If on
    TOVrec.Paras = 0             'turn off
Else                             'Else
    TOVrec.Paras = 1                'turn on
End If
ToolsOptionsView TOVrec            'Apply new setting

In the following example, Dim is used to define the dialog record Dlg for a user-defined dialog box:


Begin Dialog UserDialog 320, 57, "A Simple Dialog Box"
    Text 5, 16, 205, 18, "This is a simple dialog box."
    OKButton 220, 4, 88, 21
    CancelButton 220, 29, 88, 21
End Dialog
Dim Dlg As UserDialog            'Define the dialog record
Dialog Dlg                        'Display the dialog box

See Also

Declare, Dialog, Let, Redim