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

In general it is dangerous to assign a value (data) of one type to an object property or variable that should contain data of a different type. Often such an attempted assignment will result in an error. For example the Height property of control objects should be an integer value. Attempting to assign a string value to it as in

Text1.Height = "John"

will cause an error. Try it! However, the assignment

Text1.Top = "335.67"

will work even though the value "335.67" is a string! (an explicit string value actually)

This illustrates one of the subtleties of Visual Basic which you need to be constantly aware of. Namely Visual Basic itself will carry out conversions between data types whenever it can do so in a sensible manner. Thus in the example above the value should be an integer, and Visual Basic will convert the string to the numeric integer value 336.

Even the string value "1,001" (i.e. with a comma) would be converted to the number 1001!

Of course, if you are assigning a value to a property that should contain string data, such as the Text property of a textbox, Visual Basic will convert the value to a string. There is usually no ambiguity in converting any other data type to a string, so this will almost always work. Thus

Text1.Text = 232
Text2.Text = True
Text3.Text = 26.00231

all work because the values on the right, although not strings can all be converted to string data.