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 (continued)

Declaring Local Variables

Variables are declared in the code you write. They can be declared in two main places. One place is within a subprogram - an event for example. That is, within the

Private Sub someName( ... )
...
End Sub

block where you have so far been writing program statements. The other place is outside of any such block!

When declared within such a block the variable is called a local variable and to declare it you should write

Dim variableName As variableType

Most of the common variable types have been discussed in the previous section, and you will therefore recognise them in the examples discussed below. The variableName is a word you can choose, within certain rules and conventions. You'll be safe if you follow this prescription:

To create three local variables within the btnCalculate_Click event named year, capital and interestRate you would type the following

Private Sub btnCalculate_Click()
Dim year As Integer
Dim capital, interestRate As Double
...
End Sub

where the data types have been specified as Integer and Double respectively.

Could you have made other choices for these data types?

You would certainly not use Integer, or any other data type except perhaps Single, for the capital or InterestRate variables. These variables must store a number that has a fractional part (i.e. digits to the right of a decimal point), so only Single or Double will do. You might use the Currency data type for the variable Capital, although this would not be appropriate for the InterestRate variable, however.

For the year variable you might have chosen Short or perhaps even Byte (although if an investment is for longer than 255 years you would run into trouble).