Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 4: Data, Operations, and Built-In Functions
Last change: February 13 2016 18:47:36.

Declaring Variables

Under certain option settings in your project, variables can be used without being declared. It is a good habit, however, to declare all variables used. The reasons for this are given later in the section, “Why Force Yourself into Declaring All Variables?” To declare a variable, you can use the Dim statement. The syntax is as follows:

Dim Variable1 [As data type][, Variable2 [As data type]]...

For example, you can code the following:

Dim TheName as String, SSN as Integer Dim Rate, PresentValue As Double

The first statement declares a variable, TheName, as the String type variable, and another variable named SSN as an Integer type. The second statement declares both the variables, Rate and PresentValue, as the Double type. (Numeric data types are explained in Section 4.3, “Numeric Data and Types ,” and the String type is discussed in Section 4.5, “String Data .”)
Note that for code clarity, it is advisable to declareonly one variable per line. (Many companies have adopted this coding standard.) Thus, a better way to code the preceding first line is as follows:

Dim TheName As String Dim SSN As Integer

Initializing Value with Declaration.

When you declare only a variable per line, you can also assign an initial value to the variable as shown in the following example:

Dim TheName As String = “John Doe”

In this line, the variable TheName is declared as a String type, and is given “John Doe” as its initial value. A string variable without an initial value is initialized has a zero length string.

Using Type Suffixes

A variable or symbolic constant can also be declared with a trailing special character that signifies a data type. These special characters, such as $, %, and &, are called type-declaration characters or type suffixes, and are listed in Section 4.3. The following statement with type-declaration characters will have the same effect as the previous example.

Dim TheName$ Dim SSN%

Recall in Chapter 2, “Introduction to Visual Basic Programming,” the importance of using meaningful names for VB objects was emphasized. This observation can certainly be extended and applied to all types of names, including constants and variables. As you can see, declarations using type suffixes are not as easy to understand as those using data type names. For code clarity, always use data type names instead of type suffixes.

For program readability, always give your variables and constants meaningful names. Use the data type names instead of type suffixes to declare the data types for the variables and constants.

Checking for Variable Declarations

You have control over having VB check if the variables used in your program have been declared properly by the use of the Option Explicit statement, which has the following syntax:

Option Explicit {On|Off}

The statement must be placed before any other statement in the code window, except for other Option statements to be discussed later in this chapter. If you code,

Option Explicit Off

VB will not check whether any variable you use has been declared. The default—is Option Explicit On. It is strongly recommended that you use the default. With the default, the compiler immediately underlines any variable that is not declared.

Try This
Create a new project. Place the following code in the form Load event.
A = B
You will immediately see that both A and B are underlined. Rest the mouse pointer on the underline to see the IntelliSense indicating that the name is not declared.

Why Force Yourself into Declaring All Variables?

The advantages of declaring all variables include the following.

Ensuring Automatic “Option Explicit On” in Your Project

What if the compiler does not enforce variable declaration in your project? As stated previously, you can code the Option Explicit On statement in the code window, which is recognized as a module. The statement will be in effect for that particular module. If your project has more than one module, you will need to code the statement in each module. You can actually verify and opt to have the compiler enforce variable declaration for the entire project without any code. The settings for several Options are accessible in the Options dialog box through the Tools menu. To access the Option’s setting, follow these steps:

  1. Click the Tools menu in the IDE.
  2. Click the Options option. The Options dialog box appears.
  3. Select the Projects and Solutions option in the left frame.
  4. Click VB Defaults under the Projects and Solutions option.
  5. Select On in the combo box for Option Explicit, as shown in Figure 4-1.

Figure 4-1
Setting Option Explicit On

Experienced programmers know the importance of having all the variable names spelled correctly. A minor error in spelling a variable name can result in mysterious errors in the program, and cause numerous hours of hunting for the bug. Always ensure that Option Explicit On is the setting for your project.
Last change: February 13 2016 18:47:36.