VB Form Global Data Is Preserved After Form Unload

ID Number: Q80287

1.00

WINDOWS

docerr

Summary:

The information on page 226 of the "Microsoft Visual Basic:

Programmer's Guide" version 1.0 manual in the section titled

"Unloading Forms" implies that all data in a form is lost after the

form is unloaded using the Unload statement. However, this does not

apply to the following types of data:

- Any variable or array that is dimensioned in the general

Declarations section of the form.

- Any static variable or array that has been declared within a Sub or

Function procedure.

- Any local variable or array that has been allocated in a static Sub

or Function procedure.

The following statement on page 226 of the "Visual Basic: Programmer's

Guide" is incorrect:

Any data stored in the form is lost unless you have saved it to a

file.

This statement should be changed to read as follows:

Any data stored in the form, with the exception of static variables

and arrays, is lost unless you have saved it to a file. The values

of static arrays and variables are preserved after the form is

unloaded.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

Static data stored in a form consists of the following:

- Arrays or variables dimensioned in the general Declarations section

of a form

- Variables or arrays declared in a Sub or Function procedure using

the Static keyword

- All local variables and arrays allocated in a Sub or Function

procedure where the procedure name is preceded by the keyword

Static

All static data is allocated in a global area of memory managed by

Visual Basic. Unloading the form does not cause this memory to be

deallocated; rather, the data is preserved by Visual Basic until the

program terminates. Although the data is maintained after the form is

unloaded, you cannot access this data from any other form or module.

You must reload the form to access the data.

To demonstrate how static variables and arrays allocated from a form

do not get deallocated, do the following:

1. Run Visual Basic, or if Visual Basic is already running, choose New

Project from the File menu (ALT, F, N). Form1 will be created by

default.

2. Add a command button (Command1) to Form1.

3. Change the caption of Command1 to "Show Form Global Vars" (without

the quotation marks).

4. Add the following statements to the general Declarations section of

Form1:

Dim varX As Integer

Dim arrayX(10) As String

5. Add the following code to the Command1_Click event procedure of

Command1:

Sub Command1_Click ()

Static StaticX

StaticX = 1 'Initialize the form global variables.

varX = 10

For i = 0 To 10

arrayX(i) = Format$(i, "#0")

Next i

Unload Form1

Form1.Show 'Reload and show form.

'Values of varX and arrayX will still be

'preserved.

Print StaticX 'Print the values to the form.

Print varX

For i = 0 To 10

Print arrayX(i)

Next i

End Sub

6. Run the program (F5) and choose the Commmand1 "Show Form Global

Vars" button.

7. The values of StaticX, varX, and arrayX will print, even though the

form has been unloaded.

There is no way to cause static data in the general Declarations

section to be deallocated when the form is unloaded. For example, the

Erase statement will not cause memory to be deallocated for arrays

dimensioned in the general Declarations section. To deallocate arrays,

you must use the ReDim statement to dynamically allocate the array

when needed. To unload variables, use local variables instead of

static variables. If you use local variables instead of static

variables, the local variables are deallocated upon exit from the

procedure in which they were allocated.

Additional reference words: 1.00