|
Use of MemoryThe programs you have created so far do not do many useful things. Just clicking buttons and changing properties are not useful. The computer is very good at storing information so operations can be performed. VariablesThe term we use to contain values in memory is Variables. Variables are locations in memory. Only one value may be contained within one memory variable. There are many different types of data. There are numbers, letters, words, and combination of letters and numbers. There are also different kinds of numbers, small integers, large integers, decimal numbers. The computer needs to know what type of data is going to be stored. Therefore we need to also set up memory to store each type of information in a way that it can be retrieved easily. Therefore when we give a memory location a name, we also must declare what type of information is going to be stored. When we declare the type of variable, the computer reserves enough memory space for that particular type. This is basically the same as we do when we set fields to store data. Variable Types
Creating Variable NamesDecide on what type to use for the data you want to store. Then look at the table above for the corresponding Prefix. Then choose a descriptive name for the variable, based on what data is going to be stored. ExampleTo store some one's First Name. It would be a String Type, so the prefix would be str. strFirstName, would be an appropriate variable name. To store someone's Age. It would be a Byte Type, so the prefix would be byt. bytAge, would be an appropriate variable name. Declaring VariablesNow the magic words that prepare the computer to assign a block of memory with the name you want to give the variable. Position the insertion point after the Private Sub... line. Type Dim, a space, the name you have given the variable, As, a space, the type of the variable, and lastly a comment that explains what the variable will store. For example, to declare the variables above you would include declarations of: Dim strFirstName As String
'Employees
First name Now you have two variables declared and you can now store information in the computers memory and use the data stored there. Assigning Variables some valuesThe DIM statements have now prepared memory to store data in memory locations with the names that you have given them. Now you will learn how to assign the variables values to store. The Assignment statement is the magic way of putting a value into a variable. Begin with the variable name to assign, an equal sign, followed by the expression to store. For example to put a 17 into the variable bytAge, the following assignment statement would do this. bytAge = 17 To store my name in the variable, strFirstName, the assignment statement would look like this. strFirstName = "Dale Coleman" Notice that my name has quotes enclosing it. It is necessary to enclose certain types of data within symbols so the computer knows the data is of the correct type. Use the following chart to enclose data properly.
*Note* - The Boolean data type can only store true or false You may also store data from properties of objects, for example if the user enters their name into a text box named txtName you may store that data in a string variable called strFullName by the following statement: strFullName = txtName.Text Mathematical expressionsThe computer loves to do math! Actually that is all it can do! The mathematical operators are the symbols used to perform the various mathematical functions.
Storing the calculation in a variable is done with the use of an assignment statement. Use a variable on the left side of the equal sign and the mathematical expression on the right side. The computer will evaluate the expression and store the result in the variable. Dim sngAnswer As Single sngAnswer = 5 + 4 *(3-2/1) The code above will declare a single type variable. And store the value 9.0 in the variable sngAnswer. Notice that the result is a decimal value, this is because of the division operation that always results in a decimal. Visual Basic also follows the order of operations; with Exponentiation executed first; Multiplication, Division, Integer division and Modulus executed second; Addition and subtraction executed last. Parenthesis alter the order, with the expression deepest in the parenthesis being executed first. When you want the computer to calculate a value, it is best to declare a variable to store the result of the calculation. You must be careful that the type you specify for the variable matches the result of the calculation. For example: if you declare a variable bytAnswer as a byte type and then use the following assignment statement, bytAnswer = 1/2 then there will be an error. When ever you divide using this operator, you produce a decimal type result. Since bytAnswer is declared to hold an integer type, the computer can not perform the operation. String ExpressionsVisual Basic allows you to perform some amazing operations on strings! However, today we will look at an operation called concatenation. This operation lets you tie strings together. The symbol for concatenation is the ampersand (&) symbol. For example: Dim strFullName As String Using Variables in ExpressionsYes Virginia, your work in Algebra has not been a waste of time! The use of variables to calculate values is a fundamental task in most computer programs. This is done exactly the same as using literal values. The most important consideration, is to make sure the variables used in the calculation has a value assigned to it before it is used in the calculation. For example: (This example assumes there are two text boxes, one named txtLength, the other named txtWidth on the form, and a label named lblArea. And assumes there are valid values in the textboxes) Dim sngLength As Single Dim sngWidth As Single Dim sngArea As Single
lblArea.Caption = "The area of the rectangle is: " & sngArea Explaining the code above: The first three lines declare 3 single type variables in the three dim statements. The 4th through 8th lines stores the values in the two text boxes in variables. The 9th line multiplies the values in the variables together and stores the result in the variable sngArea. The last line concatenates a string with the value stored in the variable. Notice that there are no quotes enclosing the variable sngArea. Visual Basic looks after converting the value stored in sngArea into a string, so you do not get a type mismatch error. |