Chapter 3- Data Types and Operators Boolean Expressions Page 2 3 4

Introduction

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

Exercise 3-3: Compound Interest Calculator Version 2 (continued)

Begin a new Visual Basic project and design the form as shown in Figure 3-11 (previous page). Specify suitable names for the textboxes (i.e. do not leave the names as Text1, Text2 etc.). Also specify suitable names for the command buttons, and for the form object itself. A convention that is often used in naming control objects on a form is to use the prefixes txt for textboxes, btn for command buttons, and frm for forms. This allows the names of objects on the form to be clearly distinguished from variable names. You have probably noticed that we have been using this convention so you should use names such as txtInitialCap, txtRate, txtYear, txtInterest, and txtCapital for the textboxes and btnClear, btnNextYr, and btnExit for the buttons.

The click event for the NextYr button initiates the heart of the calculation, so lets examine the code for that first.

The first click on the button after the program starts should cause 1 to be displayed in the Year textbox, and correct values derived from the capital amount and interest rate to be displayed in the other two text boxes.

Ignore the Interest and Capital calculations for now. Focusing on the year, and following the approach used above in declaring local variables you might write

Private Sub btnNextYr_Click( ... )
Dim year As Integer

year = year + 1
txtYear.Text = year
End Sub

Note that txtYear is the name chosen for the Year textbox object, and also note that the data type for the local variable year is Integer.