Variables in Visual Basic

Variables are what separate your computer program from a being nothing but a simple calculator.   When I got variables, I got programming.  Every other concept just seemed to fall in place.  It was a real light bulb moment and I hope it has a similar effect on you.

If you are decent at maths and have a rudimentary understanding of algebra then variables should present no problems to you whatsoever.  If you don’t, and I didn’t by the way, then hopefully this simple analogy will help you.

When you have a calculator and you want to commit a number to memory, what do you press? 

calculator

Typically, to store a number to a calculator’s memory you press the MS (Memory Store) button.  Why do you want to commit it to memory?  You’d commit a number to memory to use the number later.  It saves you remembering it.  To recall your number you press the MR (Memory Recall) button.  To clear the memory so the calculator forgets this number you simply press MC (Memory Clear).  Variables allow you to commit values to memory for reuse later on.   Exactly like storing numbers in a calculator’s memory.

Variables, unlike the calculator, allow you to commit many values to memory and give these values a name.  If variables were a feature of a calculator, you’d be able to commit a number to memory, give it a name and recall it at any time (unless you happened to press MC).

Variables, unlike the calculator, allow you to commit many values to memory, and to retrieve them you give it a name when committing to memory.  If variables were a feature of a calculator, you’d be able to commit many numbers to memory, give them unique names name and recall them at any time (unless you happened to press MC).

This is a rather geeky example, but lets say you wanted to work out how much larger a 1080P resolution is than 720P.  I sadly needed to do this exact calculation about two hours ago.  I kid you not.

A 1080P television has a resolution of 1920×1080.  A 720P television has a resolution of 1280×720.  To store these resolutions in a variable, we can give them any name we want.  I’m going to call them 1080Presolution and 720Presolution.  The code to calculate how much larger 1080P is than 720P, we could use the following code.

1080Presolution = 1920 * 1080
720PResolution = 1280 * 720
Answer = 1080PResolution / 720PResolution

Note, when Visual Basic reads code, it reads and executes it line by line.  In order too.  Much like how we read these lines.  What this code is doing is creating a variable called 1080PResolution and storing the 1080P resolution in that variable.    It does likewise for 720P.  The answer divides the variables by one another.

Variables don’t have to be just numbers, they can be text or any other types of data Visual Basic deals with (we’ll be looking at the different data types in the next tutorial).  Variables are simply placeholders to store values.  Values you can name whatever you want.

In Visual Basic, unlike the pseudo-code above, you have to tell Visual Basic what the variable will be called and declare what type of data the variable will be (text, number etc).  Now this isn’t 100% true, but no way am I going to preach about development in any other way.   The declaration will inform Visual Basic that it needs to allocate a certain amount of memory for the variable.   This allows Visual Basic and hence your program to be as fast and efficient as possible which is always a good thing.

The statement we use in Visual Basic to declare a variable is Dim (or Dimension – a mathematical expression that has made its way into programming.  You’ll find a lot of this when you learn how to program, and they often make simple concepts virtually unreadable to the layman)

Create a new Visual Basic Windows Forms project in and call it “Variables”.  Add a TextBox to your Form by opening your Toolbox and selecting the TextBox from the ToolBox as shown below.  If you’re having difficulty here you could always watch the video to see how it’s done.

variablestoolbox

Resize the Textbox so the Form looks as follows

variablestextboxDouble click your form and in the Form Load Event add the following code


Private Sub Form1_Load(ByVal sender As System....) Handles MyBase.Load

     Dim resolution1080P As Integer
     Dim resolution720P As Integer

     resolution1080P = 1920 * 1080
     resolution720P = 1280 * 720

     TTextBox1.Text = (resolution1080P / resolution720P).ToString

End Sub

Run your application (press F5/press the Green Arrow in the toolbar/Click Menu Debug>>Start Debugging)

Your textbox will show the number 2.25. The resolution 1080P is 2.25 times greater than the resolution 720P.

Take a good look at the code and see if you can work out what’s happening.

The first two lines are declaring to Visual Basic that we need two variables of type Integer. An Integer is a whole number, as opposed to a number with decimal places.  Visual Basic knows that it needs a certain amount of memory to store an Integer, so it allocates this memory to the variable, even though it doesn’t know the value yet.

The next lines store the values 1920*1080 (the resolution of 1080P screens) and 1280 * 720 (the resolution of 720P) into the two variables.

The next line recalls these, divides them and displays the value in TextBox1.  The ToString instruction is quite interesting.  A TextBox, as the name suggests, displays text.  However our calculation produced a number.  Visual Basic doesn’t and can’t convert a number to text without you explicitly telling it to do so.  ToString does just that, it converts the number to text.  The exact reasons for this are explained in the next tutorial on Visual Basic Datatypes.

Does that make sense? Watch the video for a recap.

Now lets tackle the next tutorial…