next up previous
Next: 10. OptionButtons Up: VisualBasic Previous: 8. Problems with TextBoxes


9. Declaring your Variable Types: Long, Double and String, and Option Explicit

In the previous section (see [*]), we introduced the concept of different data types. There are many different types that VisualBasic understands, but we will only deal with three:

Long, Double and String

Long is for integer numbers.

Double is for real numbers (i.e. numbers which have decimal points in them!). (They can have up to around 12 significant figures.)

The String type is for character strings (like names etc). TextBoxes are defined as String types. Note though that this is done automatically for you - i.e. you don't have to declare your TextBoxes as string variables in your code with the Dim statement (see below).

It is best to get into the habit of always declaring which type each variable used belongs to. The way to do this is with the declaration or Dim statement. In the following, we are defining different variables:

Option Explicit

Private Sub Command1_Click()

Dim A, BB, I As Long
Dim x, y, speed As Double
Dim name1, place, city As String

End Sub

In the above, A, BB and I are declared to be integers (using Long); x, y and speed are declared as real numbers (using Double); and name1, place and city are declared to be string variables.

You will also notice in the above the line Option Explicit which is very useful. This line means that VisualBasic will demand that you declare every variable that you use. i.e. if you use a variable that you haven't declared with a Dim statement, it will give you an error.

You can automatically get Option Explicit to appear at the start of all of you VisualBasic Programs by going to the Tools menu item (at the top of the VisualBasic Window) and then selecting Options.... You should then tick the box labelled Require Variable Declaration. From now on, VisualBasic will insert the line Option Explicit into the top of your program.

Why do you need to declare each variable's type? The reason is that all of these variables are stored in the computer's memory in binary format - as a series bits i.e. zeros and ones. When integers are stored, they are simply converted to base two, and stored directly. i.e. $5 = 1 \times 2^2 + 0 \times 2^1 + 1 \times 2^0$. So the number 5 would be stored as "101" in the computers memory.

However, things are not this simple for real numbers. Any general real number, $R$, can be written in scientific notation as


\begin{displaymath}
R = a \times 10^b
\end{displaymath}

where $-1 \le a \le +1$ and $b$ is an integer. So when the computer stores the real number $R$, some of the bits the computer's memory are reserved for storing the $a$, and some for $b$. (In practice $a$ is only ever stored to a certain precision - typically 12 significant figures for the Double variable type.)

When the computer does algebra on variables, it obviously needs to know whether each of these numbers is stored as integers or reals because, as explained above, integers and reals are stored entirely differently. This is the reason that each variable needs to be declared!

There are three very useful data types: Long, Double and String which refer to integer, real and character string variables. You can declare your variables with a Dim ... As ... statement. Each of these variable types is stored in the computer's memory in a different format. A useful habit is to declare all your variables. You can force yourself to do this using the Option Explicit command.


next up previous
Next: 10. OptionButtons Up: VisualBasic Previous: 8. Problems with TextBoxes
Chris Allton 2006-10-27