Microsoft Visual Basic 2010 Step by Step eBook

(Tina Meador) #1

130 Part II Programming Fundamentals



  1. Click the Quit button to stop the program.


The program stops, and the development environment returns.

Variable Naming Conventions
Naming variables can be a little tricky because you need to use names that are short
but intuitive and easy to remember. To avoid confusion, use the following conventions
when naming variables:
n Begin each variable name with a letter or underscore. This is a Visual Basic
requirement. Variable names can contain only letters, underscores, and
numbers.
n Although variable names can be virtually any length, try to keep them under
33 characters to make them easier to read. (Variable names were limited to
255 characters in Visual Basic 6, but that’s no longer a constraint .)
n Make your variable names descriptive by combining one or more words when
it makes sense to do so. For example, the variable name SalesTaxRate is much
clearer than Tax or Rate.
n Use a combination of uppercase and lowercase characters and numbers.
An accepted convention is to capitalize the first letter of each word in
a variable; for example, DateOfBirth. However, some programmers prefer to use
so-called camel casing (making the first letter of a variable name lowercase) to
distinguish variable names from functions and module names, which usually
begin with uppercase letters. Examples of camel casing include dateOfBirth,
employeeName, and counter.
n Don’t use Visual Basic keywords, objects, or properties as variable names. If
you do, you’ll get an error when you try to run your program.
n Optionally, you can begin each variable name with a two-character or
three-character abbreviation corresponding to the type of data that’s stored
in the variable. For example, use strName to show that the Name variable
contains string data. Although you don’t need to worry too much about this
detail now, you should make a note of this convention for later—you’ll see it
in parts of the Visual Studio Help documentation and in some of the advanced
books about Visual Basic programming. (This convention and abbreviation
scheme was originally created by Microsoft Distinguished Engineer Charles
Simonyi and is sometimes called the Hungarian Naming Convention .)

Using a Variable to Store Input


One practical use for a variable is to temporarily hold information that was entered by the
user. Although you can often use an object such as a list box or a text box to gather this
information, at times you might want to deal directly with the user and save the input in
Free download pdf