Variables
Understanding variables and different data types
Variables
Variables are basically elements in a computer program that can take any assigned value. They can change from time to time, depending on the programmer and the user. For example, if a computer program requires a user to enter his or her name then the entity "name" will change from time to time depending on the user. The element "name" is considered a variable and can take on any value assigned to it. You will be using a lot of variables in your programming.
Data Types
Any kind of variable or element can take on a data type. There are several data types in Visual Basic. To name the most popular and the most useful are the following.
- String - this data type is made up of empty text, a single letter or several letters, or a group of words.
- Boolean - this data type is either True or False, or 1 or 0 representing the former and the latter, respectively.
- Byte - this data type can take on values from 0 to 255
- Integer - this data type can take on whole number values from -32,768 to 32,767
- Long - this data type can take on whole number values from -2,147,483,648 to 2,147,483,647
- Single - this data type can take on floating point numbers or numbers with decimals from -3.402823E38 to - 1.401298E(-45) and from 1.401298E(-45) to 3.402823E38
- Double - this data type can take on floating point numbers or numbers with decimals from – 1.79769313486231E308 to – 4.94065645841247E(–324) and from 4.94065645841247E(–324) to 1.79769313486231E308
- Variant - this data type can take on any kind of value. Use this data type sparingly because it takes up more than twice the memory of the other data types.
Declaring Variables
If you are ready to declare a variable in a program then you should use the following code:
Dim X As Integer
Once a variable is declared in a program, you or the user can utilize it appropriately.
Memory Allocation for the Variables
Variables that have more complex data types take up more memory on the computer. Using too much memory can slow down a program or in some cases, may cause a computer to hang. Use your variables wisely and declare them appropriately. For example, if you have a variable that states the number of points of a basketball player in a game then it would be wise to declare "player_points" as byte instead of integer. No basketball player in history has scored more than 255 points in a single game and there is no such thing as a negative basketball point.
www.nba.com, Wilt Chamberlain holds the NBA record for most points in a basketball game. He scored 100 points on March 2, 1962. No one has ever surpassed this mark. Michael Jordan, Karl Malone, Larry Bird, Magic Johnson, Allen Iverson, and Kobe Bryant, all-time scoring heavyweights have not been able to surpass this mark.