This blog is targeted at beginning / novice programmers for the Visual Basic (VB) language.

Friday, June 1, 2007

Variables…what are they? How do I use them?

This is quite a broad area of topic; I will do my best to keep this blog concise and cover the essential basics for the beginning programmer. Intermediate programmers will most likely find this to be a refresher course, if anything at all.

I think the best thing to start with is Microsoft's definition, this is a snippet of the definition (you can read the full definition by reading the MSDN article called, "Representing Words, Numbers, and Values with Variables"):

"A variable is a letter or name that can store a value. When you create computer programs, you can use variables to store numbers, such as the height of a building, or words, such as a person's name. Simply put, you can use variables to represent any kind of information your program needs."

As you can see from the above quote, this is a rather broad description. So let's get a little further into the idea behind what a variable does. The first thing to keep in mind; is that a variable is simply just a place-holder of something else. If you are familiar with Algebra then think of a variable as what "X" is for a number in mathematics. If you're not familiar with algebra then think of a variable as a box that can adjust in size and shape as needed. The purpose behind this container (box) is to simply provide the developer a quick word (or set of letters) that can represent anything they have stored within it. A developer could create a variable that stores a number in it (similar to "x" in mathematics), or the developer can have a single letter stored in it (such as the letter "G"), or the developer can have a complex mathematic expression stored in it (such as "52*2/16+12*32/4-2+16*48). A variable could even store a list of names (such as John, Samantha, Angelina, George, James, Jane)!

Now that you know what can be stored in a variable, the next question is what do you name a variable? Well, the simple answer is anything you want. The more complicated answer is ALMOST anything you want, there are a few set of reserved words in the programming language that you cannot use as a variables name (such as "END", "FOR", "NEXT", "ADD" as some examples). So what happens if you use one of these reserved words? You will receive a compile time error. This error will not allow your program to execute in most cases, in some cases you may still compile the application. However, should you choose to ignore this error then there is a strong possibility the application will fail when this name variable is used. This is because the computer language will not know if you are intending to use the reserved keyword or if you are intending to store something inside that word.

Since you know have a grasp on what a variable is and why you would use it, you can get a better understanding of the benefits of using variables through examples. First I will discuss how to declare a variable.

Declaring a variable is where you write a statement that says you want to use a specific word (or set of characters) to represent a specific type (such as an Integer, String, Object, List, etc). An example of declaring a variable would be to write the following statement:

    Dim testVariable as String

The above variable statement is read as "testVariable" is the variable name and "String" is the variable type. I will cover some of the different variable types available to you in a later blog. Here are some more examples of declaring variables (writing variable decleration statements):

    Dim integerVaraible as Integer

    Dim longVariable as Long

    Dim listVariable as List

    Dim arrayVariable as Array

So now that you have declared your variable, the question is how do you get what you want into that variable? That is a great question! The answer just depends on what type of variable you are declaring; this is where the MSDN website will come in very handy. Some variable declarations have certain requirements to be met prior to being used, and some have limitations on what can be placed in them. First I will start off with a very simple, and commonly used, variable assignment. We will assign a string pattern (such as this blog's name) to the variable "testVariable" (which was declared previously). Here is how you would do that:

    testVariable = "Visual Basic Helper Blog"

As you can see it was quite simple to assign the string. Now I can reference this variable throughout the project by using the word "testVariable" (which will save me a lot of typing…instead of typing out "Visual Basic Helper Blog").

I mentioned just a minute ago that some variables have pre-requisites and some have limitations. Let's take a quick look at a variable that would have a limitation. The variable types of integers are limited to storing only whole numbers, which means no letters (obviously) but also no decimal points (such as 1.86). If you try to store a letter within an integer the application will produce an error stating this is an illegal action (hence the limitation to integers). Now an example of a pre-requisite with an integer is the whole number, if you try to insert a decimal value (such as 4.79) then it will automatically get rounded off to 5.

As I mentioned previously, variables are a rather large topic and I am just brushing the basics of it. If you still need further help with understanding this concept then I strongly urge for you to get any book about Visual Basic.NET (2005) that is for the beginner. It will go into more detail than I can in this blog.

Hopefully you have a general understanding, and a solid one at that, to use variables. Remember to consult the MSDN website when you want to use a variable type that you are not familiar with!

Until next time….Happy Coding!!!


No comments: