Visual Basic 2005 Guide
Working With Variables
What Is A Variable?
When your program runs, the data that it uses is stored in main memory (RAM). The programmer makes up a name (a variable) to identify where this information is stored. The value we give to a variable can vary depending on how we write our program.
Data Types
When we use variables, we need to think about the type of data that we want to store. In Visual Basic, we will need to use the following data types.
Data Type | What Is Stored | Possible Values | Storage (bytes) |
---|---|---|---|
Integer | Whole Numbers | -2000 million to +2000 million (approx) | 4 |
Long | Whole Numbers | Very small to Very Large | 8 |
Single | Decimal Numbers | Very small to Very Large | 4 |
String | One or more characters | Any characters | 1 per char |
Date | Dates and time | Dates and times | 8 |
Boolean | True or false | True, False | 2 |
You will notice that a different data types require different amounts of storage space. This is because Visual Basic stores each data type in a different way. There are more data types, check the help file if you think you need to use a different type.
Declaring A Variable
To declare a variable you must tell Visual Basic what you want to call it and what type of data you want to store. You use the keywords Dim and As to declare a variable.
For example.
Dim intNumber As Integer
Dim sglTotal As Single
Dim dtPayment As Date
Try to use meaningful names for your variables. The variable declarations above use a prefix of 2 or 3 letters to show the data type. You cannot use spaces in the names of variables.
Using Variables In Your Code
1. Start a new project and get to the code window by clicking on View Code on the menu.
2. You should see the following lines in the code window,
Public Class Form1
End Class
3. Type the following variable declarations.
Dim intFirstNumber As Integer
Dim intSecondNumber As Integer
Dim intTotal as Integer
4. Return to the form and double click on the Form to get to the form's Load event code. Type in the following lines.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
intFirstNumber = InputBox("Enter your first number")
intSecondNumber = InputBox("Enter your second number")
intTotal = intFirstNumber + intSecondNumber
MsgBox("The total is " & intTotal)
End Sub
5. Save your work and test your program. Remember that there is a limit on the size of the Integer data type.
In addition to showing how integer variables can be declared and used in a program, this program introduces you to the most basic forms of input and output in Visual Basic. The Inputbox() and MsgBox() functions will come in useful in lots of programs.
Variable Scope
The variables that you declare in your programs have what is called scope. A variable that you declare at the top of the form (like in the program above) is called a global variable. It can be used in any of the subroutines of the program without losing its value. Any variables that you declare within a subroutine only retain their values within that subroutine. If you refer to them in another subroutine, an error is generated.