Like most programming languages, Visual Basic is able to use and process named variables and their contents. Variables are most simply described as names by which we refer to some location in memory - a location that holds a value with which we are working. It often helps to think of variables as a 'pigeonhole', or a placeholder for a value. You can think of a variable as being equivalent to its value. So, if you have a variable i that is initialized to 4, i+1 will equal 5.
In Visual Basic variables can be declared before using them or the programmer can let the compiler allocate space for them. For any but the simplest programs it is better to declare all variables. This has several benefits:
• you get the datatype you want instead of Variant,
• the compiler can sometimes check that you have used the correct type.
All variables in Visual Basic are typed, even those declared as Variant. If you don't specify
the type then the compiler will use Variant. A Variant variable is one that can hold data of any type.
30.2.1 Declaration of Variables
Here is an example of declaring an integer, which we've called some_number. Dim some_number As Integer
You can also declare multiple variables with one statement:
Dim anumber, anothernumber, yetanothernumber As Integer
This is not a good idea because it is too easy to forget that in Visual Basic the type name ap- plies only to the immediately precedingvariable name. The example declares twoVariantsand one Integer
You can demonstrate what actually happens by this little program:
Option Explicit
Public Sub main()
Dim anumber, anothernumber, yetanothernumber As Integer Dim v As Variant
Debug.Print TypeName(anumber), VarType(anumber), _ TypeName(anothernumber), VarType(anothernumber), _ TypeName(yetanothernumber), VarType(yetanothernumber)
Debug.Print TypeName(anumber), VarType(v)
Debug.Print "Assign a number to anumber" anumber = 1
Variables
Debug.Print "Assign a string to anumber" anumber = "a string"
Debug.Print TypeName(anumber), VarType(anumber)
End Sub
The result is:
Empty 0 Empty 0 Integer 2
Empty 0
Assign a number to anumber
Integer 2
Assign a string to anumber
String 8
Notice that both VarType and TypeName return information about the variable stored inside the Variant not the Variant itself.
To declare three integers in succession then, use:
Dim anumber As Integer, anothernumber As Integer, yetanothernumber As Integer
In Visual Basic it is not possible to combine declaration and initialization with variables (as VB does not support constructors - see Objects and Initialise though), the following statement is illegal:
Dim some_number As Integer = 3
Only constant declarations allow you to do this:
Const some_number As Integer = 3
In Visual Basic variables may be declared anywhere inside a subroutine, function or prop- erty, where their scope is restricted to that routine. They may also be declared in the
declarations section of a module before any subroutines, functions or properties, where they
have module level scope.
By default, variables do not have to be declared before use. Any variables created this way, will be of Variant type. This behaviour can be overrriden by using the Option Explicit statement at the top of a module and will force the programmer to declare all variables before use.
After declaring variables, you can assign a value to a variable later on using a statement like this:
Let some_number = 3
The use of the Let keyword is optional, so: some_number = 3
is also a valid statement.
The Language
anumber = anothernumber
In Visual Basic you cannot assign multiple variables the same value with one statement. the following statement does not do what a C programmer would expect:
anumber = anothernumber = yetanothernumber = 3
Instead it treats all but the first = signs as equality test operatorsand then assigns ei-
therTrueorFalseto anumber depending on the values of anothernumber and yetanotherni- umber.
Naming Variables
Variable names in Visual Basic are made up of letters (upper and lower case) and digits. The underscore character, "_", is also permitted. Names must not begin with a digit. Names can be as long as you like.
Some examples of valid (but not very descriptive) Visual Basic variable names: foo Bar BAZ foo_bar a_foo42_ QuUx
Some examples of invalid Visual Basic variable names:
2foo
must not begin with a digit
my foo
spaces not allowed in names
$foo
$ not allowed -- only letters, digits, and _
while
language keywords cannot be used as names
_xxx
leading underscore not allowed.
Certain words are reserved as keywords in the language, and these cannot be used as variable names, or indeed as names of anything else.
In addition there are certain sets of names that, while not language keywords, are reserved for one reason or another.
Conditions
The naming rules for variables also apply to other language constructs such as function names and module names.
It is good practice to get into the habit of using descriptive variable names. If you look at a section of code many months after you created it and see the variables var1, var2 etc., you will more than likely have no idea what they control. Variable names such as textinput or time_start are much more descriptive - you know exactly what they do. If you have a variable name comprised of more than one word, it is convention to begin the next word with a capital letter. Remember you can't use spaces in variable names, and sensible capital use makes it easier to distinguish between the words. When typing the variable name later in the code, you can type all in lowercase - once you leave the line in question, if the variable name is valid VB will capitalize it as appropriate.
30.2.2 Literals
Anytime within a program in which you specify a value explicitly instead of referring to a variable or some other form of data, that value is referred to as a literal. In the initialization example above, 3 is a literal. Numbers can be written as whole numbers, decimal fractions, 'scientific' notation or hex. To identify a hex number, simply prefix the number with &H.
30.3 Conditions
Conditional clauses are blocks of code that will only execute if a particular expression (the condition) is true.
30.3.1 If Statements
If statements are the most flexible conditional statements: see Branching1
30.3.2 Select Case
Often it is necessary to compare one specific variable against several values. For this kind of conditional expression the Select Case2 statement is used.
30.4 Unconditionals
Unconditionals3 let you change the flow of your program without a condition. These are the Exit, End and Goto statements.
1 Chapter 4.4 on page 32 2 Chapter 5.3 on page 35 3 Chapter 5.4 on page 35
The Language
30.5 Loops
Loops4 allow you to have a set of statements repeated over and over again.
To repeat a given piece of code a set number of times or for every element in a list: see For..Next Loops5
4 Chapter 6.2 on page 40 5 Chapter 6.1 on page 39
31 Coding Standards
See ../Credits and Permissions/1 for details of copyright, licence and authors of this piece. Work in progress. Document needs substantial reformatting to fit the Wikibooks style. Also the page is much too long for convenient editing in a browser text box so it must be broken into sections. Use the original as a guide: http://www.gui.com.au/resources/coding_ standards_print.htm
31.1 Overview
This document is a working document - it is not designed to meet the requirement that we have "a" coding standard but instead is an acknowledgement that we can make our lives much easier in the long term if we all agree to a common set of conventions when writing code.
Inevitably, there are many places in this document where we have simply had to make a choice between two or more equally valid alternatives. We have tried to actually think about the relative merits of each alternative but inevitably some personal preferences have come into play.
Hopefully this document is actually readable. Some standards documents are so dry as to be about as interesting as reading the white pages. However, do not assume that this document is any less important or should be treated any less lightly than its drier cousins.