In the previous tutorial (Visual Basic 6 Beginner Tutorial - Hello World) we learned how to create a program that when the button is clicked a message box says "Hello, World". Now we will build upon that program to learn how variables work in Visual Basic.
A variable in a programming language is similar to the way a variable works in math. If I have the equation x = 2 + y, I have two variables - x and y. These variables represent any number that can be placed in them. In the programming realm, we define a variable by a given name (for example var1) than var1 can store a value and later when we want to use that value we can call it up by its name. It makes a lot of sense once you see a few simple examples.
In Visual Basic we define a variable name by using the keyword Dim. Dim stands for "dimension" - which is a carry over from early programming days. When you define a variable, you also specify what type of data it is allowed to hold. Below is a list of all the different data types and what data they can hold.
|Boolean||True or False|
|Byte||A number 0 to 255|
|Double||A 64 bit floating point number (I.E. 3.1415) - This is used when a high degree of accuracy is needed.|
|Integer||A whole number from -32768 to 32767|
|Long||A whole number from -2,147,483,648 to 2,147,483,647|
|Single||A 32 bit number like a double, but with less pecision|
|String||A collection of letters such as "Hello". This is used to store words and sentances.|
These are the main data types built into Visual Basic, along with these are a bunch of classes Microsoft created for us to use such as Currency, Date, Time, etc. You can even create your own data types - more on this in another tutorial. There is also one other data type that is very unique. It is called the Variant type. This data type can hold any of the data above. You might immedatly ask then why don't we just always use the Variant type? The main reason is size. Variants take up more memory than any of the other data types listed above. So if you know your variable is going to be storing specific data, declare it in a way that uses the least amount of memory by specifying the type above. Usually we know what our variable will hold so we shouldn't have to use the variant type very often.
So lets take our Hello World example from before and now lets use a variable. Here is the code from before:
Private Sub Command1_Click() MsgBox "Hello, World!" End Sub
Now lets add a couple of lines and change our MsgBox function call to match this code:
Private Sub Command1_Click() Dim var1 As String var1 = "Hello, World!" MsgBox var1 End Sub
So lets go through this line by line. The first line the Visual Basic environment created for us - leav it be. Line 2 declares a variable named var1 as a string type. This means our variable can only hold strings (which are a collection of letters). Line 3 assigns the string "Hello, World!" to the variable var1. Notice that we put double quotes around the string Hello, World! this tells VB that this is a string and not some key words or other variable names. You always wrap a string in quotes.
Now that var1 contains our string we can use it in place of that string. For example the MsgBox function takes as its parameter a string. It will then display that string for us in a message box. If you remember before we called the function like this: MsgBox "Hello, World!". Now instead of doing that if you look at line 4 we simply tell MsgBox to display the string stored in var1. Lastly line 5 was created for us by the VB design environment leave it be as well.
If you now press F5 to run the program and click the command button you will see "Hello, World!" displayed in the message box once again.
Visual Basic will do something very fancy for you. It is also very annoying. If you assign a variable a value and don't declare it (using the Dim statement) visual basic will declare it for you behind the scenes as a variant. The first annoying thing about this is that the variable is a variant, and as we learned earlier this is not very efficient. But an even bigger problem exists as you develop more and more advanced programs. Let me demonstrate this in an example.
Lets say you write the above code like this:
Private Sub Command1_Click() var1 = "Hello, World!" MsgBox var1 End Sub
Line 2 of this code assigns a value to var1 (a variable that has not been declared). Visual Basic will declare the variable behind the scene as a variant. Then when in Line 3 we call MsgBox with that variable it works great. If you run this program it will work just like before, and Hello, World will be displayed. Everything seems great, but lets say we make a mistake like this code has done:
Private Sub Command1_Click() var1 = "Hello, World!" MsgBox var2 End Sub
If you notice I accidentally told the MsgBox to display a variable named var2 instead of var1. Now this is absurd because I don't even have a variable named var2. Now what I would expect to have happen is that when I run this program, an error message would pop up that says there is no such thing as var2 and tell me what line this error occured. Than I would go to that line see the mistake I made and change var2 to var1. However, since VB automatically creates variables for me it instead will create a variable named var2 and it will have no value assigned to it. So the result is a message box that says nothing.
Now imagine my program is thousands of lines of code long. I run it and click on the button and this message box with nothing in it shows up. Now I am stuck going through all my code trying to figure out where I used a variable name that is incorrect. This is a very annoying feature indeed. Because of this any Visual Basic programmer that is worth his weight in salt will put the Option Explicit clause at the top of all their programs. This tells Visual Basic that you will always explicitly declare your variables. Than if VB finds a variable that was not declared it will throw an error so you can easily discover it and fix it. Here is what our code looks like with this line added:
Option Explicit Private Sub Command1_Click() Dim var1 As String var1 = "Hello, World!" MsgBox var2 End Sub
Now if you again run the program - it will throw an error telling you the variable is not defined.
And it will go to the line where var2 is located and highlight it for you. You can than fix it by changing var2 to var1, hit F5, and the program will work as planned.
Since option explicit is really the only way to program, Microsoft has given you a preference in the VB IDE where you can tell it to automatically add option explicit to your code. I highly recommend setting this by going to the menu bar at the top of the screen and selecting Tools -> Options. On the first tab (Editor) you will notice a check box that says Require Variable Declaration. Check this box and from then on VB will put Option Explicit on top of all your programs.