Wednesday 24 June 2009

Statics and Constants in VB6

Statics in Visual Basic
Statics allow you to maintain a variables value even when it goes out of scope. For example:
Suppose you have a Sub in a form called CountThem that looks like this:

Private Sub CountThem()

Dim intI As Integer
Static intJ As Integer
intI = intI + 1
intJ = intJ + 1
Print intI, intJ

End Sub

Suppose you have some other Sub that calls CountThem three times in a row:

Call CountThem
Call CountThem
Call CountThem

The following output would be displayed on the form (by default, the "Print" statement directs its output to the current form):

(values for intI)
(values for intJ)
1
1
1
2
1
3

Note that the "regular" variable, intI, declared with "Dim", does not retain its value between calls, whereas the Static variable, intJ, does.

Note: The keyword "Static" can also be used in the Sub procedure header, which causes all variables in that procedure to be static. Example:

Private Static Sub AllVarsAreStatic()
Dim intCounter As Integer ' as if declared Static
Dim strErrMsg As String ' as if declared Static
. . . ' other statements
End Sub

Constants
VB supports the use of symbolic, or named constants. Constants are similar to variables, except that you provide a value for the constant when you declare it, and its value can never change. The syntax for declaring a constant is:

[Public Global Private] Const constantname [As datatype] = expression

A global (project-level) constant can only be declared in a standard (.bas) module (not a form), using "Public Const" or "Global Const" (the "Public" keyword is preferred). Module-level constants can be declared in the General Declarations Section of either a standard or form code module using "Private Const" (or just "Const"; the default is "Private"). Local-level constants are declared in any procedure of a standard or form module just using the word "Const" (no "Public" or "Private"). If you omit the "As datatype" clause, VB will use its "best guess" as to what the datatype should be, based on the expression.

The following table shows how constants may be declared and the location of their declaration affects the scope:


Keyword Used to Declare the
Constant:
$
Where Declared
à

General Declarations Section of a Form (.frm) Module

General Declarations Section of a Standard (.bas) Module

Sub or Function procedure of a Form or Standard Module
Const
module-level scope
module-level scope
local-level scope

Private Const
module-level scope
module-level scope
not allowed

Public Const
-or –
Global Const

not allowed

project-level scope

not allowed


Sample Constant Declarations:

Public Const gsngTAX_RATE As Single = 0.06

Private Const mdtmCUT_OFF_DATE As Date = #1/1/1980#

Const strERROR_MESSAGE = "Invalid Data" 'String data type assumed

Note that in Const declarations, string literals are delimited with double quotes ("), date literals are delimited with pound signs (#), and numeric literals are not delimited.

Naming Conventions for Constants

Similar to naming variables, use the lowercase three-character datatype prefix ("int", "str", etc.), prefixed by an "m" if a module-level constant or "g" if a global (project-level) constant. However, for the main part of the name, use all capital letters (with underscores to break up individual words within the name).

No comments:

Post a Comment