| Chapter 3- Data Types and Operators | Boolean Expressions Page 2 3 4 |
| The If Statement Page 2 3 4 5 6 7 8 9 | |
| Arithmetic Operations Page 2 3 4 5 6 | Boolean Operators and Nested If Statements Page 2 3 4 5 6 7 |
| Events and Sequential Processing Page 2 3 4 5 | More Examples Page 2 3 4 5 6 7 8 9 10 11 12 |
| Datatypes and Conversions Page 2 3 4 5 6 7 | Using Check Box and Option Controls Page 2 3 4 5 6 7 8 9 10 |
| Variable Declarations - Local and Global Page 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Exercises Page 2 3 4 5 6 7 8 |
| Chapter 4- Selection Statements | Review Questions |
| Introduction | |
3-5: Variable Declarations (continued)
Exercise 3-2 Revisited
Open Exercise 3-2 from the last section, and display the code window for the form. The click event for the Calculate button should be modified to use local variables. The idea is that data (values) will be obtained from the textboxes and assigned to variables, calculations will be done using the variables, and the results will be assigned to the Text properties of other textboxes. The Text properties themselves will not be directly used in the calculations because they are in fact string data which would need to be converted, and besides the properties of control objects (primarily the Text property) should generally only be used for input and output and not for storing data to be used in calculations.
The values from the Text properties of the Initial Capital and Interest Rate textboxes are stored in variables by this code
Private Sub btnCalculate_Click( ... )
Dim capital, interestRate As Double
capital =
Convert.ToDouble(txtInitialCap.Text)
interestRate =
CDbl(txtRate.Text)
...
End Sub
Note that having declared the variables capital and interestRate we can then assign values to them. The values must be of the same type as the variables to which they are assigned. This is ensured by using the functions that convert the string values of the Text properties to the appropriate type.
Next we would like to calculate the interest amount after one year. We will use the expression interestRate * capital, i.e. the value represented by the interestRate variable multiplied by the value represented by the capital variable.
But what should we do with the value resulting from this expression? We want to display it in the Interest1 textbox, and we want to add it to the capital amount to calculate the new capital at the end of year one. We could write
Private Sub btnCalculate_Click( ... )
Dim capital, interestRate As Double
capital =
Convert.ToDouble(txtInitialCap.Text)
interestRate =
CDbl(txtRate.Text)
txtInterest1.Text = interestRate *
capital
txtCapital1.Text = capital +
CDbl(txtInterest1.Text)
...
End Sub
but the last line (assigning a value to txtCapital1.Text) is essentially using the Text property of the txtInterest1 textbox as a place to store the interest amount value - something that we are trying to avoid.
We should in fact declare a new variable, thereby creating a space in the computer's memory in which to store the value for the amount of interest. Thus we should write
Private Sub btnCalculate_Click( ... )
Dim capital, interestRate, interestAmount As Double
'
Assign values from textboxes to variables ...
capital =
Convert.ToDouble(txtInitialCap.Text)
interestRate =
CDbl(txtRate.Text)
' For year 1 ...
interestAmount =
interestRate*capital
capital = capital + interestAmount
txtYear1.Text =
1
txtInterest1.Text = interestAmount
txtCapital1.Text =
capital
...
End Sub
Notice too that we have begun to use comments in this program, i.e. the lines beginning with the ' character. These are ignored by the computer and are meant to inform a human reader, perhaps another programmer who is trying to understand and modify your work.