Variables and Data Types |
|
|
Variable Declaration |
Unlike languages referred to as strongly typed,
Visual Basic is so flexible you can use any variable just by
specifying its name. When you provide this name, the computer
directly creates an area in memory for it. Based on this, consider
the following code section:
If you execute this program, when the form displays, it would be painted in red. If the user clicks the form, it would be painted in blue. If the user presses a key, the form would be painted in green. There is some confusion in the program. It uses a variable that seems to have a name but initialize three times with different colors. Visual Basic allows you to directly use any name for a variable as you see fit. Fortunately, to eliminate the possibility of this confusion, you can first let Visual Basic know that you will be using a certain variable. Informing Visual Basic about a variable prior to using that variable is referred to as declaring a variable. When a variable has been declared, just like the variable not declared, the computer reserves an area of memory for it. To declare a variable, type the Dim keyword, like this: Dim On the right side of Dim, you must type a name for the variable, following the same rules we reviewed above. Here is an example of declaring and using a variable: Private Sub Form_Load() Dim BackgroundColor BackgroundColor = vbRed BackColor = BackgroundColor End Sub Declaring a variable simply communicates to Visual Basic the name of that variable. You can still use a mix of declared and not-declared variable. This is demonstrated in the following event:
Once again, the compiler believes that you are using two variables; one is called BackgroundColor and the other is called SomeColor. This can still create a great deal of confusion because you may be trying to use the same variable referred to twice. The solution to this possible confusion is to tell Visual Basic that a variable cannot be used if it has not been primarily declared. To communicate this, on top of each file you use in the Code Editor, type Option Explicit This can also be done automatically for each file by checking the Require Variable Declaration in the Options dialog box. |
Practical Learning: Using a Variable |
|
Introduction to Data Types |
Introduction |
When you decide to use a variable, you are in fact asking the computer to use a certain amount of space to hold that variable. Since different variables will be used for different purposes, you should specify the kind of variable you intend to use, then the computer will figure out how much space is needed for a particular variable. Each variable you use will utilize a certain amount of space in the computer's memory. Before declaring or using a variable, first decide what kind of role that variable will play in your program. Different variables are meant for different situations. The kind of variable you want to use is referred to as a data type. To specify the kind of variable you want to use, you type the As keyword on the right side of the variable's name. The formula to declare such a variable is: Dim VariableName As DataType Once you know what kind of variable you will need, choose the appropriate data type. Data types are organized in categories such as numbers, characters, or other objects. |
String |
A string is an empty text, a letter, a word or a group of words considered. To declare a string variable, use the String data type. Here is an example: |
Private Sub Form_Load() Dim CountryName As String End Sub
After declaring the variable, you can initialize. If you want its area of memory to be empty, you can assign it two double-quotes. Here is an example: |
Private Sub Form_Load() Dim CountryName As String CountryName = "" End Sub
If you want to store something in the memory space allocated to the variable, assign it a word or group of words included between double-quotes. Here is an example: |
Private Sub Form_Load() Dim CountryName As String CountryName = "Great Britain" End Sub
You can also initialize a string variable with another. |
Boolean |
A Boolean variable is one whose value can be only either True or False. To declare such a variable, use the Boolean keyword. Here is an example: |
Private Sub Form_Load() Dim IsMarried As Boolean End Sub
After declaring a Boolean variable, you can initialize by assigning it either True or False. Here is an example: |
Private Sub Form_Load() Dim IsMarried As Boolean IsMarried = False End Sub
Like any other variable, after initializing the variable, it keeps its value until you change its value again. |
Numeric Data Types |
Introduction |
A natural number is one that contains only one digit or a combination of digits and no other character, except those added to make it easier to read. Examples of natural numbers are 122, 8, and 2864347. When a natural number is too long, such 3253754343, to make it easier to read, the thousands are separated by a special character. This character depends on the language or group of language and it is called the thousands separator. For US English, this character is the comma. The thousands separator symbol is mainly used only to make the number easier to read. To support different scenarios, Microsoft provides different types of natural numbers |
Byte |
A byte is a small natural positive number that ranges from 0 to 255. A variable of byte type can be used to hold small values such as a person's age, the number of fingers on an animal, etc. To declare a variable for a small number, use the Byte keyword. Here is an example: |
Private Sub Form_Load() Dim StudentAge As Byte End Sub
Integer |
An integer is a natural number larger than the Byte.
It can hold a value between To declare a variable of type integer, use the Integer keyword. Here is an example: |
Private Sub Form_Load() Dim MusicTracks As Integer End Sub
Long Integer |
A long integer is a natural number whose value is between –2,147,483,648 and 2,147,483,642. Examples are the population of a city, the distance between places of different countries, the number of words of a book. To declare a variable that can hold a very large natural number, use the Long keyword. Here is an example: |
Private Sub Form_Load() Dim Population As Long End Sub
Decimal Data Types |
Introduction |
A real number is one that displays a decimal part. This means that the number can be made of two sections separated by a symbol that is referred to as the Decimal Separator or Decimal Symbol. This symbol is different by language, country, group of languages, or group of countries. In US English, this symbol is the period as can be verified from the Regional (and Language) Settings of the Control Panel of computers of most regular users: On both sides of the Decimal Symbol, digits are used to specify the value of the number. The number of digits on the right side of the symbol determines how much precision the number offers.
|
Single |
A single is a decimal number whose value can range from –3.402823e38 and –1.401298e-45 if the number is negative, or 1.401298e-45 and 3.402823e38 if the number is positive. To declare a variable that can hold small decimal numbers with no concern for precision, use the Single data type. Here is an example: |
Private Sub Form_Load() Dim CountryName As String Dim IsMarried As Boolean Dim StudentAge As Byte Dim Tracks As Integer Dim Population As Long Dim Distance As Single End Sub
Double |
While the Single data type can allow large numbers, it offers less precision. For an even larger number, Microsoft Visual Basic provides the Double data type. This is used for a variable that would hold numbers that range from 1.79769313486231e308 to –4.94065645841247e–324 if the number is negative or from 1.79769313486231E308 to 4.94065645841247E–324 if the number is positive. To declare a variable that can store large decimal numbers with a good level of precision, use the Double keyword. |
In most circumstances, it is preferable to use Double instead of Single when declaring a variable that would hold a decimal number. Although the Double takes more memory spaces (computer memory is not expensive anymore(!)), it provides more precision. |
Here is an example of declaring a Double variable:
Private Sub Form_Load() Dim Distance As Double End Sub
Currency |
The Currency data type is used for a variable that can hold monetary values. To declare such a variable, use the Currency keyword. Here is an example: |
Private Sub Form_Load() Dim CountryName As String Dim IsMarried As Boolean Dim StudentAge As Byte Dim Tracks As Integer Dim Population As Long Dim Distance As Single Dim StartingSalary As Currency End Sub
Other Data Types |
Date |
A date is a numeric value that represents the number of days that have elapsed since a determined period. A time is a numeric value that represents the number of seconds that have elapsed in a day. To declare a variable that can hold either date values, time values, or both, use the Date keyword. After the variable has been declared, you will configure it to the appropriate value. Here are two examples: |
Private Sub Form_Load() Dim CountryName As String Dim IsMarried As Boolean Dim StudentAge As Byte Dim Tracks As Integer Dim Population As Long Dim Distance As Single Dim StartingSalary As Currency Dim DateOfBirth As Date Dim KickOffTime As Date End Sub
Object |
An Object is almost anything else that you want to use in your program. If you don't specify a data type or can't figure out what data type you want to use, you can use the Variant keyword or let Visual Basic use the Variant data type. |
Variant |
A Variant can be used to declare any kind of variable. You can use a variant when you can't make up your mind regarding a variable but, as a beginning programmer, you should avoid it. Here is a table of various data types and the amount of memory space each one uses: |
|
Using Variables |
Details on Declaring Variables |
We have learned how to declare a variable as follows: Dim CountryName As String We also saw that we can declare different variables each on its own line as follows; Dim FirstName As String Dim LastName As String Dim Salary As Currency Dim AlreadyVisited As Boolean If you have many variables of the same data type, you can declare them on the same line, each separated with a comma. Remember to specify their common type. Here is an example: Private Sub Form_Load() Dim CountryName, Address, City, State As String Dim IsMarried As Boolean Dim StudentAge As Byte Dim Tracks As Integer Dim Population As Long Dim Distance As Single Dim StartingSalary, WeeklyEarnings As Currency Dim DateOfBirth, KickOffTime As Date End Sub When naming your variables, besides the above suggestions, you can start a variable's name with one to three-letter prefix that could identify the data type used. Here are a few suggestions: |
|
Constants |
Introduction |
A constant is a value that doesn't change. There are two types of constants you will use in your programs: those supplied to you and those you define yourself. |
The Carriage Return-Line Feed Constant |
Visual Basic provides the vbCrLf constant. It is used to interrupt a line of code and move to the next line. |
Built-in Constants: PI |
PI is a mathematical constant whose value is approximately equal to 3.1415926535897932. It is highly used in operations that involve circles or geometric variants of a circle: cylinder, sphere, cone, etc.
|
|
||
Previous | Copyright © 2004-2014 FunctionX, Inc. | Next |
|