A variable is a piece of data that can change its value during the course of a program's execution. Variables are used to store values that are needed for a program to run successfully.
Before you can use a variable in a program you need to declare it. This means you need to tell the computer that you are going to use a particular data type (the computer then knows how much memory it needs to set aside to store this value).
In Visual Basic you declare a variable using the following syntax:
Dim myVariableName As DataType = Value Here are some examples: Dim yearGroup_int As Integer = 12 Dim firstName_str As String = "Archibald" Dim height_dbl As Double = 176.32 Dim gender_char As Char = "M" Dim boarder_bool As Boolean = False NB You can declare a variable without setting an initial value... Dim surname_str As String Dim DOB_date As Date
The value of a variable can be set or changed at any time during the program's execution.
For instance, I might want to write a simple program to multiply two numbers together that have been entered by the user, and then output the result. My program might look something like this:
Dim firstNumber As Integer Dim secondNumber As Integer Dim result As Integer firstNumber = txt_firstNum.Text secondNumber = txt_secondNum.Text result = firstNumber * secondNumber txt_OutputText = result
Depending on where a variable is declared, the variable can be seen by other elements of the program code. There are two types of variable declarations: Global and Local.
Global variables are declared in the root area of the program code (generally at the beginning of your program code). The values stored in these variables will be accessible from any area of the code. This can be useful if you need to access a variable from anywhere in the program, but if you declare global variables when you don't need to access them all the time, they will sit and take up main memory, thus wasting precious computing resources. Use with care.
Local variables are declared in a sub area of the program code, this may be in a function or procedure. The values stored in these variables will only be accessible from the sub program they are declared in. This can be friendly on computing resources as they are declared when the sub program is called, used, and when the sub program is exited, destroyed. So you only use main memory when you need to store the variable and it does not waste main memory like global variables do. However if you think you will need to variable outside the sub program you should think of using a global variable. If you want to quickly tell the difference between a global and a local variable use these quick rules.
Constants are exactly the same as variables apart from one rather important difference....
You cannot change the value of a constant!
The value of a constant remains the same throughout the execution of the program. You can declare a constant like this:
Const VAT_const As Double = 20.0
Constants are useful for storing values that you know will never change during the execution of a program, such as VAT rate, and will require less memory too! Other reasons to use them include: