19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

44 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

■ DECLARING VARIABLE TYPES<br />

So far, we have not distinguished between variables that hold strings <strong>and</strong> variables that hold<br />

numbers. There are several advantages to specifying the type of values, string or numeric, that<br />

can be assigned to a variable. A statement of the form<br />

Dim variableName As String<br />

specifies that only strings can be assigned to the named variable. A statement of the form<br />

Dim variableName As Single<br />

specifies that only numbers can be assigned to the named variable. The term Single derives from<br />

single-precision real number. After you type the space after the word “As,” the editor displays<br />

a list of all the possible next words. In this text we use only a few of the items from this list.<br />

A Dim statement is said to declare a variable. From now on we will declare all variables.<br />

However, all the programs will run correctly even if the Dim statements are omitted. Declaring<br />

variables at the beginning of each event procedure is regarded as good programming<br />

practice because it makes programs easier to read <strong>and</strong> helps prevent certain types of errors.<br />

EXAMPLE 5<br />

The following rewrite of Example 3 declares all variables.<br />

Private Sub cmdCompute_Click()<br />

Dim interestRate As Single<br />

Dim principal As Single<br />

Dim phrase As String<br />

picBalance.Cls<br />

interestRate = 0.0655<br />

principal = 100<br />

phrase = “The balance after a year is”<br />

picBalance.Print phrase; (1 + interestRate) * principal<br />

End Sub<br />

Several Dim statements can be combined into one. For instance, the first three Dim<br />

statements of Example 5 can be replaced by<br />

Dim interestRate As Single, principal As Single, phrase As String<br />

<strong>Visual</strong> <strong>Basic</strong> actually has several different types of numeric variables. So far, we have<br />

used only single-precision numeric variables. Single-precision numeric variables can hold<br />

numbers of magnitude from as small as 1.4 � 10 -45 to as large as 3.4 � 10 38 . Another type<br />

of numeric variable, called Integer, can hold only whole numbers from –32768 to 32767.<br />

Integer-type variables are declared with a statement of the form<br />

Dim intVar As Integer<br />

The Integer data type uses less memory than the Single data type <strong>and</strong> statements using the<br />

Integer type execute faster. (This is only useful in programs with many calculations, such as<br />

the programs in later sections that use For...Next loops.) Of course, Integer variables are limited<br />

because they cannot hold decimals or large numbers. We will use Integer variables extensively<br />

with For...Next loops in Section 6 <strong>and</strong> occasionally when the data clearly consist of<br />

small whole numbers.<br />

Other types of numeric variables are Long, Double, <strong>and</strong> Currency. We do not use them<br />

in this text. If you want to learn about them, consult Appendix C. Whenever we refer to a<br />

numeric variable without mentioning a type, we mean Single or Integer.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!