Visibility and Lifetime of Variables


Every language has rules for defining the visibility and the lifetime of variables and procedures. Basic is no exception, although unfortunately the rules seem to change with each version. Throughout the twisted history of the language, attempts to add new features without breaking old ones have created a mess. The Basic landscape is dotted with abandoned scope and visibility modifiers
such as Shared, Common, and now Global. In the current version, even the best intentions have not made matters any clearer. Consider the following.


The Dim keyword (the least mnemonic name in the language) creates a variable with local visibility and temporary lifetime when used within a procedure (except that lifetime is permanent if the procedure is declared to be static), but when used outside a procedure, Dim creates a variable with private visibility and permanent lifetime (despite the fact that lifetime is meaningless in a standard module, although it is important in a class or form module). Private and Public are declarators for variables, but they are modifiers for constants, declarations, types, and procedures.


If you can understand that and if you always use Dim correctly, you don’t need this book. For the rest of us, it might help to create a myth and pretend to believe it. Here are the “commandments” that are embodied in my Basic data myth:
For those literal-minded readers who want facts rather than opinions, Table 1-1 lists the public, private, and default visibility for the various elements you can declare in your programs.

Standard Module Form and Class Modules
Constants Default private
Private OK


Public OK

Default private
Private OK


Public illegal

User-defined types Default public
Private OK


Public OK

Default illegal
Private required


Public illegal

Declare statements Default public
Private OK


Public OK

Default illegal
Private required


Public illegal

Variables Default private*
Private OK


Public OK

Default private*
Private OK


Public creates property

Functions and subs Default public
Private OK


Public OK

Default creates method
Private OK


Public creates method

Properties Default public
Private OK


Public OK

Default public
Private OK


Public OK



* Default means declaring with Dim rather than with Public or Private.


Table 1-1. Public, private, and default visibility of module elements.


One of the most dangerous errors in Visual Basic occurs when you try to put several declarations on one line. For example:

Dim c, i, h As Long

You believe that you’ve defined three Longs, but you haven’t. Instead, you have two variants (the default type for the first two) and one specifically declared Long. Here’s another variation:

Dim c As Long, i, h

Watch Your Types


Pascal programmers might be more likely to write the first line, whereas C programmers might tend to write the second. In either case, the problem is particularly dangerous because the code will work correctly 90 percent of the time, although perhaps not efficiently.