Declaring and Naming Variables

function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window.open(url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); } function Print() { window.focus(); if(window.print) { window.print(); window.setTimeout('window.close();',5000); } }
Team-Fly    

Special Edition Using Microsoft® Visual Basic® .NET
By Brian Siler, Jeff Spotts
Table of Contents
Chapter 6.  Storing Information in Variables


The first line of code is a Dim, or Dimension, statement, used for dimensioning (or declaring) variables. When your program declares a variable, it is, in essence, telling Visual Basic, "Set aside a memory location that will be used to store variable information; I will reference it by this name." The last two words in the Dim statement tell Visual Basic what type of information you plan to store in this case, integer numbers. This information helps determine how much memory is to be set aside for the variable.

Note

Variables also can be declared with other keywords besides Dim, such as Public and Private. For more details, see the section in Chapter 8, "Managing Program Tasks with Procedures," called "Understanding Scope and Accessibility."


Naming Conventions

In naming a variable, you have a tremendous amount of flexibility. Variable names can be simple, or they can be descriptive of the information they contain. The example at the beginning of the chapter uses a classic rock song reference, which is a syntactically legal name but not very descriptive of the variable's actual purpose! As a general rule, you should make your variable names descriptive enough to make your code easy to read, but also keep the names as short as possible to make the code easy to type. For example, if we are using a variable to store the number of seats remaining, the following two variable declarations describe that fact clearly:

 Dim NumberOfSeats As Integer  Dim SeatsRemaining As Integer 

Although you are allowed great latitude in naming variables, you must adhere to a few restrictions:

  • The name must start with a letter, not a number or other character.

  • The remainder of the name can contain letters, numbers, and/or underscore characters. No spaces, periods, or other punctuation characters are allowed.

  • The name must be unique within the variable's scope. (Scope refers to the context in which the variable is defined, as you will learn in Chapter 8.)

  • The name cannot be one of Visual Basic's reserved words.

Tip

After you have declared a variable and are referencing it in code, such as an assignment statement, the Ctrl+Space shortcut key can be used to complete the variable name after typing only a few characters. By making your variable names unique as possible within a small number of characters, you can type the variable names very quickly using Ctrl+Space.


To make sure variable names make sense to a programmer reading the code, an organization may want to establish a naming convention. A naming convention is just an agreed-upon way of assigning variable names so you can tell more about them. Table 6.1 lists a sample naming convention that uses a variation of the Hungarian style naming convention. This type of naming convention adds a lowercase prefix to the variable name, to indicate its type.

Table 6.1. Sample Variable Naming Prefixes

Variable Type

Prefix

Example

String

str

strFirstName

Integer

int

intAge

Long Integer

lng

lngPopulation

Double

dbl

dblThrustRatio

Boolean

b

bTaxable

Frequently programmers also use additional prefix letters to indicate the scope of the variable, as in the following examples:

 Dim lstrUserID As String   'l indicates local variable  Dim astrUserID As String   'a indicates function argument  Dim mstrUserID AS String   'm indicates module-level 

Starting with Visual Studio .NET, Microsoft seems to be getting away from prefix-oriented naming conventions. Many of the classes simply use descriptive names with uppercase at the beginning of each word. Hovering the mouse over a variable in code displays the variable's declaration statement in a tooltip, making it easy to immediately know the variable's data type. The samples listed here are just samples; a good guideline is to be consistent within your development group.

Changes to the Dim Statement

Visual Basic .NET introduces some changes to the way variables can be declared. Multiple declarations of the same type are now easier, as in the following example:

 Dim x, y As Integer 

In the previous line of code, x and y are both Integer variables. In Visual Basic 6.0, declaring two Integer variables in the same Dim statement required specifying the type after each variable, as follows:

 Dim x As Integer, y As Integer 

Starting in Visual Basic .NET, both of the previous two code statements will create Integer variables x and y.

Another new feature is the ability to initialize a variable within the Dim statement. Initializing a variable simply means assigning it a starting value, and is frequently performed with an assignment statement immediately after the declaration, as in the following example:

 Dim BottlesOfBeer As Integer  BottlesOfBeer = 99 

Starting with Visual Basic .NET, you can replace the previous two lines of code with a single declaration and initialization statement:

 Dim BottlesOfBeer As Integer = 99 

It should be noted that initialization only works for one-variable Dim statements.

Note

Although there is a default value for new variables (0 for numbers), it is good programming practice to initialize your variables rather than depending on this default value.



    Team-Fly    
    Top
     



    Special Edition Using Visual Basic. NET
    Special Edition Using Visual Basic.NET
    ISBN: 078972572X
    EAN: 2147483647
    Year: 2001
    Pages: 198

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net