Microsoft KB Archive/108709

= Expected Expression Error: Dynamic Array Not OK in User- Type =

Article ID: 108709

Article Last Modified on 12/9/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition

-



This article was previously published under Q108709



SYMPTOMS
An "Expected: expression" error occurs within a Type statement in Visual Basic when you follow an array name with empty parentheses.

You can use the Type ... End Type statement block to define your own data type using Basic's predefined data types as components. You can dimension variables or arrays using that user-defined type.



CAUSE
Visual Basic interprets the empty parentheses that follow a variable name in the Type statement as a declaration of a dynamic array. Visual Basic does not allow dynamic arrays to be declared within a user-defined Type statement block.



RESOLUTION
Within the Type ... End Type statement block, parentheses that follow a variable name must contain a number to indicate the number of elements in a static array. Visual Basic allows Type statements to contain static arrays but not dynamic arrays.

NOTE: Once you correctly define a user-defined type, you can dimension a dynamic array of that type. See further below for an example.



STATUS
This behavior is by design.



Steps to Reproduce Behavior
 Start a new project in Visual Basic. Form1 is created by default. From the File menu, choose New Module.  Add the following code to the new module, MODULE1.BAS: Type newinfo tarray as string 'Gives "Expected: Expression" error on numstores As Long End Type  Edit the line containing tarray. Then select any other line. The automatic syntax checker in Visual Basic correctly highlights the and gives the following error:

Expected: Expression

Running the program by pressing the F5 key also correctly reports this syntax error. To correct this programming error, add a number of array elements in the empty parentheses. For example, change tarray to tarray(10). This changes the array from dynamic to static.</li></ol>

Visual Basic interprets the empty parentheses in tarray in the Type statement as a declaration of a dynamic array. Visual Basic does not allow dynamic arrays to be declared within a user-defined Type ... End Type statement block. The parentheses must contain a number to indicate the number of elements in a static array.

How to Make a Dynamic Array of User-Defined Type
<ol> Start a new project in Visual Basic. Form1 is created by default.</li> From the File menu, choose New Module.</li>  Add the following code to the new module, MODULE1.BAS: Type newinfo tarray(20) As String 'Static array declared in user-defined type numstores As Long End Type </li>  Double-click Form1 to display the form's code window. Add the following to the form load event: Sub Form_Load ' Use ReDim to declare or redimension a dynamic array: ReDim arrayx(20) As newinfo 'Make dynamic array of user-defined type arrayx(18).tarray(12) = "Ruby slippers"      ' Assign value. arrayx(18).numstores = 999                   ' Assign value. form1.Show ' In load event, must Show form before Print can work. Print arrayx(18).tarray(12)  ' Print value. Print arrayx(18).numstores   ' Print value. End Sub </li></ol>

NOTE: You cannot change the number of elements in static arrays at run time, but you can use the ReDim statement to change the number of elements in dynamic arrays.

<div class="references_section">