How to Initialize Static Variables to Nonzero Values

Last reviewed: June 21, 1995
Article ID: Q119737
The information in this article applies to:
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0
  • Microsoft Visual Basic Programming System for Windows, version 1.0

SUMMARY

Normally in Visual Basic, when a static variable is declared inside a Function or Sub procedure, it gets initialized to 0 (numeric data type) or an empty string, "" (string data type), by default. This article shows a way to initialize these variables to values other than the default.

MORE INFORMATION

When a variant variable is first declared, it is initialized with the EMPTY value by default. The idea here is to take advantage of this fact, by testing a dummy variant static variable with the IsEmpty function and initializing the other static variables to the values you want if this function returns TRUE. It is important to set the dummy variant to some value after you initialize your static variables, so that IsEmpty always returns FALSE for subsequent calls to the subroutine, and that because of this the other static variables are not reinitialized.

Here is an example that shows how to initialize a static variant, integer, and string variable declared in a Sub called InitStatic:

Step-by-Step Example

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. On Form1, add a command button (Command1).

  3. Add the following code to the "(general) (declarations)" section of Form1:

       Sub initstatic ()
          Static Dummy, V, I As Integer, S As String
    
          ' The code in the following if statement will be executed only once:
          If IsEmpty(Dummy) Then
             ' Initialize the dummy variant, so that IsEmpty returns FALSE for
             ' subsequent calls.
             Dummy = 0
    
             ' Initialize the other static variables.
             V = "Great"
             I = 7
             S = "Visual Basic"
          End If
    
          Print "V="; V, "I="; I, "S="; S
    
          ' Static variables will retain their values over calls when changed.
          V = 7
          I = I + 7
          S = Right$(S, 5)
       End Sub
    
    

  4. Add the following code to the Command1_Click() event:

       Sub Command1_Click()
          initstatic
       End Sub
    
    

  5. Run the program (press the F5 key). Then double-click the Command1 button to see the results.


Additional reference words: 1.00 2.00 3.00
KBCategory: kbprg kbcode
KBSubCategory: PrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.