Declaring 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 | Private] Const constantname [As datatype] = expression


Constants are preferred for two reasons:


1.      Value of a constant can never change: This is the safety feature. Once the constant has been declared you cannot change its value from anywhere.


2.     Constants are processed faster than variables: When the program is running, the values of constants don’t have to be looked up. The compiler replaces the constant names with their values, thus saving the program execution time.


A global (project-level) constant can only be declared in a standard module (not a form), using "Public Const".  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 .NET will use its "best guess" as to what the datatype should be, based on the expression.



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, I am using a 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). As mentioned earlier, the use of the “Hungarian” prefix in the name is a subjective one, and the practice of naming variables in this manner is a topic of debate. However, the practice of using all capital letters with underscores is quite common when defining a constant.



Declaring Arrays


Arrays are declared in the same manner as other variables, except that the array bounds are coded in parentheses following the variable name.


For fixed-length arrays, you must specify the upper bound of the array in parentheses; for variable-length arrays, you do not specify any bounds; you just code an empty set of parentheses. In .NET, the lower bound of any dimension of an array is always zero, so if you declare a one-dimensional array with "x" as the upper bound, you get "x + 1" elements, indexed zero to "x".


Arrays can have up to 32 dimensions (although it is rare that you would have more than three dimensions; one or two is the norm). 


The syntax for declaring an array is:


[Dim | Private | Public ] arrayname(upperbound[, …])   [As datatype]



Sample declarations:


Array Declaration


Dim aintCount(9) As Integer

declares a 10-element array, indexed 0 to 9

Dim aintCount() As Integer

declares a variable-length array whose upper bound will be determined at run-time


To refer to an individual element of an array in a procedural statement, place the desired index in parentheses next to the array name. For example, the syntax refers to the 5th element of the array aintCount:




Following are some examples of multi-dimensional array declarations:


The following declares a 2-dimensional array (4 rows indexed 0 to 3, by 5 columns indexed 0 to 4):


Dim asngSales(3, 4) As Single


The following declares a 3-dimensional array (the first dimension has 4 elements indexed 0 to 3, within that, the second dimension has 12 elements indexed 0 to 11, and within that, the third dimension has 5 elements indexed 0 to 4):


Dim asngResults(3, 11, 4) As Single


To refer to an individual element of a multi-dimensional array in a procedural statement, place the desired indices in parentheses next to the array name (you must have one index per dimension, separated by commas).  Examples:


      asngSales(2, 3)

      asngResults(0, 11, 2)



VB6 and earlier allowed for some syntax in declaring arrays that is no longer valid in VB.NET:


(1)        VB6 and earlier had the Option Base statement, which allowed you to specify whether the default lower bound of an array would start from 0 or 1. This is no longer supported in VB .NET. In VB.NET, arrays always start at index zero.


(2)        In VB6 and earlier, you could optionally specify the lower bound of a dimension of an array; and this lower bound could be any integer value, not just 0 or 1. For example, declarations such as

                        Dim aintCount (1 To 10) As Integer                                           ' ILLEGAL IN VB.NET


                        Dim asngResults(0 To 2, 3 To 12,  -4 To 8) As Single    ' ILLEGAL IN VB.NET

            are legal in VB6 and earlier, but not in VB .NET.



Declaring Structures

(Formerly Known As "User-Defined Types" or "UDTs")


Structures are VB.NET's way of implementing data structures. In VB6 and earlier, these were known as User-Defined Types (UDTs) and were declared with the keyword Type rather than Structure. In C/C++, they are called structs, in Pascal and COBOL they are called records (they are also called "group" items in COBOL).


The (simplified) syntax for defining a structure is:


      [Public | Private] Structure StructureName

          [Dim | Public | Private] Variable1  As datatype


          [Dim | Public | Private] Variablen  As datatype

      End Structure


Note: In previous versions of VB, the variables within a UDT did not use a scope specification (i.e., Dim, Public, Private, etc.). In VB.NET, the scope specification is required (and he keyword “Dim” is usually fine for this purpose). 


For example, to define a structure for an employee record, you might code the following:


      Public Structure EmployeeRecord

          Dim strEmpName As String

          Dim dtmHireDate As Date

          Dim sngHourlyRate As Single

      End Structure


However, the definition alone is not enough. The Structure definition is basically a "template" on which other variables are defined; the template itself does not store any data. To use a structure, you must define a variable "As" the name following the keyword "Structure" (in this case, "EmployeeRecord"). For example:


                Dim udtEmpRec As EmployeeRecord


The above defines a variable called "udtEmpRec" which has the attributes defined by the structure "EmployeeRecord". Thus, it is "udtEmpRec" which you refer to in your procedural statements, NOT "EmployeeRecord". To reference an individual element of the structure, you must qualify that element with the "udt" variable that you defined. For example, the following code places data in the individual elements of udtEmpRec:


      udtEmpRec.strEmpName = "JOE SMITH"

      udtEmpRec.dtmHireDate = #1/15/2001#

      udtEmpRec.sngHrlyRate = 25.50


You can declare any number of variables "As" the structure that you have defined. For example:


      Dim udtEmpRec2 As EmployeeRecord

      Dim audtEmpRec(10) As EmployeeRecord


Note that the second definition above declares an array of the EmployeeRecord type. To refer to an individual field (such as strEmpName) in a particular "record number", you would code something like the following:


      audtEmpRec(5).strEmpName = "BILL JONES"


You can also have an array at the elementary level. For example:


      Public Structure EmployeeRecord

          Dim strEmpName As String

          Dim dtmHireDate As Date

          Dim sngHourlyRate As Single

          Dim dblQuarterlyEarnings(4) As Double

      End Structure



If you have this declaration:


      Dim udtEmpRec As EmployeeRecord


Then a valid reference would be:


      Msgbox udtEmpRec.dblQuarterlyEarnings(2)


If you have this declaration:


      Dim udtEmpRec(5) As EmployeeRecord


Then a valid reference would be:


      Msgbox udtEmpRec(3).dblQuarterlyEarnings(2)


You can have nested structures. Consider the following declarations:


      Public Structure EmployeeName

          Dim FirstName As String

          Dim MidInit         As String

          Dim LastName  As String

      End Structure


      Public Structure EmployeeRecord

          Dim udtEmpName As EmployeeName

          Dim dtmHireDate As Date

          Dim sngHourlyRate As Single

          Dim dblQuarterlyEarnings(4) As Double

      End Structure


      Dim udtEmpRec As EmployeeRecord


You would reference the EmployeeName fields as follows: