Typed Variable with String in Type Losing Value After Sub Call

ID Number: Q81468

1.00

WINDOWS

buglist1.00

Summary:

Under very specific conditions in Visual Basic, a local variable of

user-defined type that has one field that is a string variable will

lose its value. This only happens under specific conditions, and in

most cases works as expected. The steps to reproduce the problem must

be followed exactly for the program to behave in this manner.

Microsoft has confirmed this to be a problem in Visual Basic

programming system version 1.0 for Windows. We are researching this

problem and will post new information here as it becomes available.

More Information:

Workaround

----------

A workaround is to declare the variable using the keyword Static.

Steps to Reproduce Problem

--------------------------

The following conditions must be met for a user-defined type variable

to lose its value (in a field of type string) after being passed to a

Sub procedure:

- The user-defined type is declared in the global module

and has more than one field, where one of these fields is

a string variable and it is not the first field.

- A variable of this type is locally dimensioned in an event

procedure or Sub and passed to another Sub (Var1 in this

example).

- In the second Sub, a locally dimensioned variable of this same type

is assigned to Var1.

After exiting the second Sub procedure, the original variable string

field does not contain the string that was passed to it.

1. In the Visual Basic 1.0 programming environment, add the following

declaration to the global module:

Type recordtype

first As Integer

second As Integer

third As String

End Type

2. Using the mouse, double-click on the form to bring up the

Form_Click event procedure Code window, and add the following code:

Sub Form_Click ()

Dim thisvar As recordtype

Sub1 thisvar 'call Sub1 and pass it thisvar

Print thisvar.first 'Print the values in thisvar after returning

Print thisvar.second ' from Sub1

Print thisvar.third ' this one will print as a blank

End Sub

3. In the general Declarations section, add the following to add a Sub

procedure called Sub1:

Sub Sub1 (avar As recordtype)

Dim tempvar As recordtype

tempvar.first = 5

tempvar.second = 3

tempvar.third = "bontoo"

avar = tempvar 'assign the value of tempvar to avar, the passed

'variable

End Sub

5. From the Run menu, choose Start to run the application.

6. Click on the form to trigger the Form_Click event.

Note: The output on the form will appear as follows:

5

3

The third field did not print "bontoo" as expected.

Changing the Form_Click procedure as follows solves the problem:

Sub Form_Click ()

Static thisvar As recordtype 'Note the change here. The variable

'thisvar is declared as static.

Sub1 thisvar 'Call Sub1 and pass it thisvar.

Print thisvar.first 'Print the values in thisvar after

Print thisvar.second ' returning from Sub1

Print thisvar.third

End Sub

The output will now be as expected:

5

3

bontoo

Additional reference words: 1.00