Difference between revisions of "Declaring Visual Basic Variables and Constants"

From Techotopia
Jump to: navigation, search
(Understanding Variable and Constant Scope)
(Understanding Variable and Constant Scope)
Line 74: Line 74:
 
There are four levels of scope in a Visual Basic application:
 
There are four levels of scope in a Visual Basic application:
  
* Block Level Scope
+
=== Block Level Scope ===
  
 
When a variable or constant is declared in a distinct code structure (such as an ''If'' statement or ''Do loop''), the variable is only visible and accessible to the code within that structure. For example, in the following code the ''intCustCount'' variable is only accessible to code within the ''If'' statement:
 
When a variable or constant is declared in a distinct code structure (such as an ''If'' statement or ''Do loop''), the variable is only visible and accessible to the code within that structure. For example, in the following code the ''intCustCount'' variable is only accessible to code within the ''If'' statement:
Line 87: Line 87:
 
</pre>
 
</pre>
  
* Procedure Level Scope
+
=== Procedure Level Scope ===
  
 
Variables and constants declared within a Visual Basic procedure (i.e a Function or Subroutine) are only visible and accessible to code within that procedure (for details of Visual Basic procedures see [[Visual Basic Modules and Procedures]]. For example, the variable ''intResult'' in the following code is only visible to code within the function:
 
Variables and constants declared within a Visual Basic procedure (i.e a Function or Subroutine) are only visible and accessible to code within that procedure (for details of Visual Basic procedures see [[Visual Basic Modules and Procedures]]. For example, the variable ''intResult'' in the following code is only visible to code within the function:
Line 103: Line 103:
 
</pre>
 
</pre>
  
* Module Level Scope
+
=== Module Level Scope ===
  
 
When a variable or constant is declared outside of any procedures or code structures in a Module it is deemed to have module level scope. This means that the variable or constant is visible to all Visual basic code contained in the same module, regardless of whether that code is located in a procedure or not. There is a section at the top of each module called the ''Declarations Section'' for this purpose. The Declaration section is located above all procedures and code in a module.
 
When a variable or constant is declared outside of any procedures or code structures in a Module it is deemed to have module level scope. This means that the variable or constant is visible to all Visual basic code contained in the same module, regardless of whether that code is located in a procedure or not. There is a section at the top of each module called the ''Declarations Section'' for this purpose. The Declaration section is located above all procedures and code in a module.
  
* Global Scope
+
=== Global Scope ===
  
 
When a variable or constant is declared as ''global'' it is visible to all procedures and modules that comprise the application. Global variables and constants, as with Module level variables, must be declared outside of any procedures in teh Declarations section and must use the ''Public'' keyword. The syntax for declaring a global variable is as follows:
 
When a variable or constant is declared as ''global'' it is visible to all procedures and modules that comprise the application. Global variables and constants, as with Module level variables, must be declared outside of any procedures in teh Declarations section and must use the ''Public'' keyword. The syntax for declaring a global variable is as follows:

Revision as of 18:52, 1 August 2007

The different types of Visual Basic variables and constants was described in detail in the previous chapter (Understanding Visual Basic Variable & Constant Types). In this chapter we will learn how to declare and initialize variables and constants in Visual Basic.

Before learning how to declare variables and constants it is worth a quick reminder of the difference between variables and constants in Visual Basic. Both constants and variables provide a way to reserve memory locations for the storage of values of various types (such as numbers, characters and strings). These memory locations are assigned names which can be used in Visual Basic code to reference these memory locations. For example, you might want to store the interest rate for a banking application. To do so you would declare a variable with a name of your choice (such as interestRate) and specify the variable as an Interger type (since it will be storing a number). Having declared the variable you can assign a value to it and read that value anywhere in your application code.

The difference between variables and constants is that the value stored in a variable can be changed at any time after it has been created. The value assigned to a constant, as the name suggests, cannot be changed once it has been declared an initialized.


Contents


Declaring Visual Basic Variables

Variables are declared using the Visual Basic Dim keyword. The syntax for a simple declaration of a variable is as follows:

Dim variableName As variableType

In the above outline, Dim is the keyword which indicates to Visual Basic that a variable is being declared. variableName is the name assigned to the variable. Try to use a descriptive variable name and prefix the name with something which indicates the variable type. For example, when declaring a String variable prefix the name with str (e.g strFirstName). The As keyword precedes the declaration of the variable type (String, Date, Integer etc). For a complete list of Visual Basic variable types see Understanding Visual Basic Variable & Constant Types.

To declare an Integer value named intInterestRate for example:

Dim intInterestRate As Integer

It is also possible to declare multiple variables on the same line by separating each varaible by a comma. When the variable are of the same type, the type declaration only needs to be made once at the end of the declaration:

Dim intInterestRate, intExchangeRate As Integer

In the case where the variables are of different types the type of variable must be decalred at the end of each group of the same type. For example:

Dim strCustomerName As String, intInterestRate, intExchangeRate As Integer

Initializing Visual Basic Variables

Visual Basic variables may be initialized either during the declaration, or after the declaration. Unless there is a good reason to do otherwise, it is recommended that variables be initialized during the declaration.

Initialization is performed using the Visual Basic assignment operator (=). To initialize a single variable when it is declared:

Dim intInterestRate As Integer = 5

When declaring multiple variables each variable may be initialized in the declaration line:

Dim strCustomerName As String = "Fred", intInterestRate = 5 As Integer, intExchangeRate As Integer = 10

Assigning New Values to Visual Basic Variables

Once a variable has been declared, a new value can be assigned to the variable at any time using the variable name and the assignment (=) operator. In the following sample Visual Basic code, a variable is declared and initialized to 10. It is then re-assigned the value of 20:

Dim intInterestRate As Integer = 10

intInterestRate = 20

Referencing Variable Values

Accessing the value of a variable in Visual Basic code is as simple as typing the variable name. For example, the following code example adds 20 to the value of intInterestRate and assigns the result to the intNewRate variable:

Dim intInterestRate As Integer = 10
Dim intNewRate As Integer = 0

intNewRate = intInterestRate + 20

Understanding Variable and Constant Scope

A variables and constants can be declared anywhere in a Visual Basic project. It is important to understand, therefore, that where and how the variable is declared dictates the scope of that variable. The scope of a variable relates to where else in the application source code the variable is accessible.

There are four levels of scope in a Visual Basic application:

Block Level Scope

When a variable or constant is declared in a distinct code structure (such as an If statement or Do loop), the variable is only visible and accessible to the code within that structure. For example, in the following code the intCustCount variable is only accessible to code within the If statement:

If blnNewCustomer Then
    Dim intCustCount As Integer
    For intCustCount = 1 to 50
         Do ReadCustRecord()
    Next InCustCount
End if

Procedure Level Scope

Variables and constants declared within a Visual Basic procedure (i.e a Function or Subroutine) are only visible and accessible to code within that procedure (for details of Visual Basic procedures see Visual Basic Modules and Procedures. For example, the variable intResult in the following code is only visible to code within the function:

Public Function PercentageOf(ByVal value1 As Integer, ByVal value2 As Integer) As Integer

        Dim intResult As Integer

        intResult = (value1 / value2) * 100

        Return intResult

End Function

Module Level Scope

When a variable or constant is declared outside of any procedures or code structures in a Module it is deemed to have module level scope. This means that the variable or constant is visible to all Visual basic code contained in the same module, regardless of whether that code is located in a procedure or not. There is a section at the top of each module called the Declarations Section for this purpose. The Declaration section is located above all procedures and code in a module.

Global Scope

When a variable or constant is declared as global it is visible to all procedures and modules that comprise the application. Global variables and constants, as with Module level variables, must be declared outside of any procedures in teh Declarations section and must use the Public keyword. The syntax for declaring a global variable is as follows:

Public variableName As dataType

Similarly, a global constant is defined using the following syntax:

Public Const constName As dataType

Declaring and Referencing Visual Basic Constants

Constants, once declared and initialized cannot be changed (hence the name constant' and are declared using the Visual Basic Const keyword. The syntax for declaring variables is as follows:

Const constName As datatype = value.

For example, to declare a String constant named companyName with a value of Techotopia the declaration would read as follows:

Const companyName As String = "Techotopia"

A Visual Basic constant is referenced using the name defined when the constant was declared. For example, the following Visual Basic code sets the Text roperty of a Label control to the string value contained in the companyName constant:

Const companyName As String = "Techotopia"

companyLabel.Text = companyName

Declaring and Referencing Visual Basic Constants

Constants, once declared and initialized cannot be changed (hence the name constant' and are declared using the Visual Basic Const keyword. The syntax for declaring variables is as follows:

Const constName As datatype = value.

For example, to declare a String constant named companyName with a value of Techotopia the declaration would read as follows:

Const companyName As String = "Techotopia"

A Visual Basic constant is referenced using the name defined when the constant was declared. For example, the following Visual Basic code sets the Text roperty of a Label control to the string value contained in the companyName constant:

Const companyName As String = "Techotopia"

companyLabel.Text = companyName