Variables
In this tutorial we will be looking at what variables are and how you can use them. Unlike the other two tutorials we will not be creating a program in this one but I would recommend trying them out, maybe in the program from the previous tutorial.
What is a variable?
In essence a variable is a piece of stored data. Variables do not exist after the program has ended. They are stored in the section of RAM allocated to your program. There are different types of variables for different types of data e.g. text and numbers. I have listed the visual basic data types later in this tutorial but you will find that almost every programming language has slightly different variables.
How to make a variable
In Visual Basic you will find that if you do not formally declare variables they will still work (partly) as there is a general data type which can hold any type of data. This is very bad practice however for many reasons which I won't detail here. If you type 'Option Explicit' at the very beginning of the code then you will be forced to declare every variable or an error will be generated. To declare a variable we use the 'Dim' statement in this form:
Dim VariableName As VariableType
. The types of variables in visual basic are:| Variable type | Storage size | Type of data |
|---|---|---|
| Byte | 1 byte | Integer 0 to 255 |
| Boolean | 2 bytes | True or False |
| Integer | 2 bytes | Integer -32768 to 32767 |
| Long (long integer) | 4 bytes | Integer -2,147,483,648 to 2,147,483,647 |
| Single | 4 bytes | Decimal -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values |
| Double | 8 bytes | Decimal -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values |
| Currency | 8 bytes | 4 decimal places -922,337,203,685,477.5808 to 922,337,203,685,477.5807 |
| Decimal | 14 bytes | +/-79,228,162,514,264,337,593,543,950,335 with no decimal point; +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; |
| Date | 8 bytes | up to the year 9999 |
| String | 10 bytes + string length | 2 billion characters roughly |
| Variants | 16/22+ bytes (numbers/characters) | double with numbers, string with characters. You shouldn't use these as they are inefficient |
Variables can be declared at the very top of the document before anything else or inside functions or subs. Subs or functions are thing containing code which is run when they are called. We used them when we wrote code which happened when the user clicked the button. The function is the code between the two lines of automatically generated code. If variables are declared inside a function then they cannot be accessed by other functions and they are deleted when the function ends. This means that if the user clicks a button and a variable is declared inside the function then the variable will be gone the next time the button is clicked.
To allow other functions to use variables declared inside a function we can declare them as public variables. To do this we replace 'Dim' with 'Public'.
Public VariableName As VariableType
Setting the variables value
Well so far we know what variables are and how to declare them. This is not so much use as empty variables don't really do much. Setting a variables value is very easy and is done just as you would expect (hopefully).
VariableName = VariableValue
You can also set variables from objects properties and visa versa.
MyString = Text1.Text Text1.Text = MyString
© Jonathan Waller 2005; QuantumState Visual Basic




