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-4: Data Types in Visual Basic (continued)

Understanding Exercise 3-2

In Exercise 3-2 you have values that are obtained from the Text property of text boxes - for example, txtInitialCap.Text and txtRate.Text. Although these are string values we then attempted to use them with the multiplication operator, i.e. we wrote the expression txtRate.Text * txtInitialCap.Text.

The multiplication operator expects numbers and therefore Visual Basic attempts to convert the string values to numbers. Provided you have typed characters in the textboxes that can be converted to a number this in fact succeeds and the multiplication succeeds.

Try entering 5,500 in the initial capital textbox. Can you explain what happens? How about 2.342E3 in the initial capital textbox?

The expression txtInitialCap.Text + txtInterest1.Text also uses the string values from the Text properties of the textboxes. However, now an important difference arises. The + operator has a dual meaning. Not only does it mean addition for numbers but it also means catenation (or joining together) for string data. Thus Visual Basic does not attempt to convert the strings to numbers, it simply catenates the strings!

Thus, in the exercise, when txtInitialCap.Text = 2000 and txtInterest1.Text = 100 we get 2000100 rather than 2100 for the value in the txtCapital1 textbox!