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 lightbulb 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 however (I didn’t by the way), then no fears, 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?  Typically it’s the MS button.  Why do you want to commit it to memory?  To reuse later after you’ve entered more calculations.  To recall your number you press the MR button.  To clear the memory you simply press MC.  Variables, just like when working with a calculator, allow you to commit values to memory for reuse later on.   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 a number to memory, give it a name and recall it 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.  Yes, I’m that guy…

Anyhow lets have two variables 1080P (which has a resolution of 1920×1080) and 720P (which has a resolution of  1280×720).  We can give these variables any names we want.  I’m going to call them 1080Presolution and 720Presolution.

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

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 datatypes in the next tutorial).  Variables are simply placeholders to store values for use later on.  Placeholders 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.  Now this isn’t 100% true, but no way am I going to preach about development in any other way.   A declaration of the variable will inform the compiler that it needs to allocate a certain amount type of memory for the variable and the variable will be a certain type.   This allows the complier 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 – again another mathematical expression that has made its way into programming, making 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 can’t do this, watch the video to get a better idea.

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

     TextBox1.Text = resolution1080P / resolution720P

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 trailing decimal places.

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

The next line recalls the values of the variables, divides them and displays the value in TextBox1.

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

Now lets tackle the next tutorial on Visual Basic Datatypes.