Programming & Scripting Tutorials

Visual Basic: Variables





Think of variables as boxes - you can put things in the box, and you can also change what is inside the box.
In Visual Basic you create different types of variables that hold different types of information (a box that can only hold a certain type of object).

In Visual Basic we create variables using the "Dim" keyword, then the variable name, followed by "As" then the variable type (for examaple Integer (whole number)).
Here is an example:

Dim Num As Integer


This creates the variable "Num" and declares it as an integer. This empty box can now hold whole number values (if we so choose it to).
Another type of varible is a string, this holds a sequence of text:

Dim Name As String


The above creates the variable "Name" and declares it as a string (text).

Now we can create empty boxes, we need to put values into the boxes.
We do this by simply stating the variables name, followed by the equals sign, followed by the value we want to set it to.
For example, this is an example of setting Num to an integer value:

Num = 2010


This sets the variable "Num" to 2010.

We can also set strings using similar methods (but the value we want to set it to must be in quotation marks):

Name = "Joe"

As expected, the above sets the variable "Name" to "Joe".

This Visual Basic tutorial was written by


Back to Visual Basic

Advertisement: