Intro to Variables
[ Using VB ]
Before you can begin programming, you must understand the concept of variables. Variables are the storage media within programs. For example if you wanted the program to know the users name, then you might declare a variable called strName, ask the user for their name, and then store the result into the strName variable.
Declaring a variable
Standard Naming Conventions
Common Data Types Defined
Using Variables
Arrays, and Redim
Before you can use a variable, you have to declare the variable. In Visual Basic you use the Dim statement to declare variables. Some common variable types are Integer, and String. This is how you use the Dim statement.
Dim [variable name] As [variable type]
Examples:
Dim strName As String
Dim intCounter As Integer
You may wonder why the variable strName has the prefix 'str', and isn't just called Name, or why the variable intCounter has the prefix 'int'. The prefixes are part of the standard naming conventions used by many programmers. The prefix 'str' stands for String, and means that the variable is of type String, and the prefix 'int' represents Integer. Here is a list of the standard prefixes for the different types of variables that are used in Visual Basic. Don't worry about what all of the variables are just yet, you won't need most of them for a while.
These are the prefixes for the data types used in Visual Basic.
| Data Type | Prefix | Example |
| Boolean | bln | blnFound |
| Currency | cur | curTotal |
| Date (time) | dat | datStart |
| Double | dbl | dblTolerance |
| Error | err | errNumber |
| Integer | int | intQuantity |
| Long | lng | lngDistance |
| Object | obj | objCurrent |
| Single | sng | sngAverage |
| String | str | strFullName |
| User-Defined type | udt | udtEmployee |
| Variant | vnt | vntCheckSum |
These are the prefixes for the control types in Visual Basic.
| Control | Prefix | Example |
| Checkbox | chk | chkReadOnly |
| Combo box | cbo | cboPhoneNumbers |
| Common Dialog control | dlg | dlgFileOpen |
| Communications | com | comFax |
| Control | ctr | ctrCurrent |
| Command Button | cmd | cmdExit |
| Data Control | dat | datBiblio |
| Directory list box | dir | dirSource |
| Drive list box | drv | drvTarget |
| File list box | fil | filSource |
| Form | frm | frmCalculator |
| Frame | fra | fraLanguage |
| Gauge | gau | gauStatus |
| Graph | gra | graRevenue |
| Grid | grd | grdPrices |
| Horizontal Scroll Bar | hsb | hsbVolume |
| Image | img | imgIcon |
| Label | lbl | lblHelpMessage |
| Line | lin | linVertical |
| List Box | lst | lstNames |
| MDI Child Form | mdi | mdiNote |
Menu |
mnu | mnuFileOpen |
| OLE Control | ole | oleWorksheet |
| Option Button | opt | optRed |
| Picture | pic | picVGA |
| Text Box | txt | txtLastName |
| Timer | tmr | tmrAlarm |
Menu naming conventions.
| Menu Caption Sequence | Menu Handler Name |
| Help | mnuHelp |
| Contents | mnuHelpContents |
| File | mnuFile |
| Open | mnuFileOpen |
| SendFax | mnuFileSendFax |
| SendEmail | mnuFileSendEmail |
| Exit | mnuFileExit |
These naming conventions are used so that when you are reading someones code, or even looking over your own code you can tell what kind of data a variable is supposed to hold. When declaring variables, you should use a name that is descriptive of what data that variable will be used for. Such as if you wanted to store the users name, then you might declare a variable called 'strUserName', or if you wanted to store the score of the game you might use a variable called 'intScore'.
These are the most commonly used data types.
Integer: the most commonly used data type. It can only store whole number integers within the range -32,768 to 32,767.
Long: same as Integer, but with a range of -2,147,483,648 to 2,147,483,647. For most practical uses Integer will do just fine, and there is no need for using Long, however there are some cases when the range of Integer just won't cut it.
String: the second most commonly used data type. It stores a "String" of characters. In Visual Basic, a string is contained within quotes "".
Single: represents a floating point number, or in other words a number with a decimal. It has a range of -3.402823e38 to -1.401298e-45 for negative numbers, and 1.401298e-45 to 3.402823e38 for positive numbers.
Double: same as Single except with a range of -1.79769313486232e308 to -4.94065645841247e-324 for negative numbers, and a range of 4.94065645841274e-324 to 1.79769313486232e308 for positive numbers. Double like Long is just a bigger variable type, in most instances there is no need for the precision that the double types can support, but in some cases it may be necessary.
Boolean: represents either True or False
You may be thinking, "O.K. I know what variables are, now how do I use them?". Well it's actually quite a simple process. First of all to assign a value to a variable you use the '=' command.
Example:
strName = "No Name"
intTotal = 3
For the Integer, Single, and other numerical data types, you can use the standard mathematical symbols '+', '-', '/', and '*' for addition, subtraction, division, and multiplication. Visual Basic follows the same order of operations, as you will find in any algebra book.
Examples:
intTotal = intPart1 + intPart2
sngAverageGrade = (sngGrade1 + sngGrade2 + sngGrade3) / 3
To concatenate strings (or form one string containing both smaller strings), you simply add them using the '+' operator.
If you had a string containing "Hello", and another containing "World", and wanted to have one string that contained "Hello World!", then you would do this.
strHelloWorld = strHello + " " + strWorld + "!"
Arrays are a useful data type, because they can store multiple
different variables of the same type. Arrays are declared using the Dim statement.
Example:
Dim strNames( 0 to 4 ) As String 'Array of five strings
Dim intGrades( 1 to 10 ) As Integer 'Array of 10 integers
The Specs:
Dim varname( lower to upper ) As vartype
Variable length arrays:
Dim sngAverages() As Single 'Variable length array of singles
Then use the ReDim statement.
ReDim sngAverages( 0 to 20 ) As vartype
This can be very useful if you aren't sure exactly how many elements you need in an array when you are writing the program. You can just load a few variables, then ReDim the array to make room for the data that you need to put in the array.