Chapter 1 - Variables


Dust off your knowledge of algebra. Remember variables?

x + y = 7

Programming uses variables pretty extensively. Beyond that, the level of math required really depends on you. Here's something that doesn't make sense algebraically:

x = x + 2

Here's the way you read that: Set the new value of x to the old value of x plus two. VB doesn't require that you use single letter variables. You can have variables with huge names (just remember that you have to keep typing it):

theareaoftherectangle = therectanglewidth * therectangleheight

You can also mix the use of case for readability, but variable names aren't case sensitive in VB - that is, the variable 'theareaoftherectangle' is the same as 'TheAreaOfTheRectangle' - it's just that the latter is a little easier to read.
Variable names can also include numbers - note, however, that the variable name cannot start with a number: width2 is valid, but 2width is not.
Variable names can also include some punctuation, but not really very many of them - most of the punctuation characters are used to mean something else - freds_variable is ok, but fred's_variable isn't - the apostrophe tells VB "the rest of this line is a comment so ignore it".

Variables can contain numeric values or literal values. The first is a numeric, as the above ones. The second is a literal:

  theWidth = 10
  myName = "Joe"

Before you use a variable, you should declare it. VB actually allows you to get away without doing that, but I think it's a horrid programming practice. You can tell VB to not allow you to do it by typing:

Option Explicit

at the top of each form or module (those are covered later). Then VB will inform you if you try to use a variable that you haven't declared. You declare variables thusly:

Dim myName

Dim is short for dimension. "But why", you ask, "do I need to declare my variables?" My answer is that it will save you several hours of debugging if you ever mistype a variable name. Let's say you are using a variable with name rwidth in your 80,000 line program. In one spot you accidentally type rwidht instead. You run your program, and your calculation keeps coming up zero. Trying to figure out why and where could be quite a chore. On the other hand, if you tell VB to stop you when you try to use a variable that hasn't been defined, it would highlight the misspelling for you the next time you tried to run the program.

Variable Scope

Skipping ahead for just a moment, here is what a function looks like in VB:

  Dim width
Dim height
 
Function CalculateArea(width, height)
Dim area
 
area = width * height
 
End Function

As you can see, a variable by the name of area was declared at the top of the function. This is called a local variable, because it is local to the function. It only lives while the function is executing. The variables width and height are declared outside the function. These variables live the whole time the program is running. Since area dies at the end of the function, other functions don't have any access to its value. In the example above, you would never find out what the result of the multiplication is.

If you have variables declared both inside and outside functions, like this:

  Dim area
 
Function CalculateArea(width, height)
Dim area
 
area = width * height
 
End Function

Although they have the same name, those are actually two different variables. The variable that is declared inside the function is actually the one that is getting the result of the multiplication. The variable that is declared outside the function will have the same value after the function runs as it did before. More than likely your intent would be to modify the one outside. If so, then don't declare the one inside the function. Since the outside one lives for the duration of the program, you can use or display its value elsewhere, after the calculation function runs.

A VB program can be (and usually is) comprised of more than one file on your hard drive. It is made up of 'forms' and 'modules'. If you declare a variable in one module, it is not available to other modules - if you're using Option Explicit, VB will report the missing variable.

But what if you want to share the variable across multiple files? VB has a way for you to do this. Instead of declaring the variable with Dim, you declare it with Global:

Global area

Now the variable is accessible across files.