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.

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

6. Most major programming languages require that all variables be declared<br />

before they can be used. Although declaring variables with Dim statements is<br />

optional in <strong>Visual</strong> <strong>Basic</strong>, you can tell <strong>Visual</strong> <strong>Basic</strong> to make declaration m<strong>and</strong>atory.<br />

The steps are as follows:<br />

(a) From any code window, click on the down-arrow to the right of the Object<br />

box <strong>and</strong> click on (General).<br />

(b) Type<br />

Option Explicit<br />

<strong>and</strong> press Enter.<br />

Then, if you use a variable without first declaring it in a Dim statement, the<br />

message “Variable not defined” will appear as soon as you attempt to run the<br />

program. One big advantage of using Option Explicit is that mistypings of variable<br />

names will be detected. Otherwise, malfunctions due to typing errors are<br />

often difficult to detect.<br />

7. You can have <strong>Visual</strong> <strong>Basic</strong> automatically place Option Explicit in every program<br />

you write. The steps are as follows:<br />

(a) Press Alt/T/O <strong>and</strong> click on the Editor tab to invoke the editor options.<br />

(b) If the square to the left of “Require Variable Declaration” does not contain<br />

a check mark, click on the square <strong>and</strong> press the OK button.<br />

8. Variables that are not (explicitly) declared with Dim statements are said to be<br />

implicitly declared. Such variables, which have a data type called Variant, can<br />

hold strings, numbers, <strong>and</strong> several other kinds of information.<br />

9. You can display the type of a variable with the following steps—position the<br />

cursor over the word, press the right mouse button, <strong>and</strong> click on Quick Info.<br />

10. Val can be applied to strings containing nonnumeric characters. If the beginning<br />

of the string str represents a number, then Val(str) is that number; otherwise, it<br />

is 0. For instance, Val(“123Blastoff ”) is 123, <strong>and</strong> Val(“ab3”) is 0.<br />

11. The KeyPress event also applies to comm<strong>and</strong> buttons <strong>and</strong> picture boxes.<br />

12. Concatenation of strings also can be represented by a plus sign (+). However,<br />

restricting the plus sign to operations on numbers eliminates ambiguity <strong>and</strong> provides<br />

self-documenting code.<br />

13. If Val is omitted from the statement<br />

numVar = Val(txtBox.Text)<br />

or Str is omitted from the statement<br />

txtBox.Text = Str(numVar)<br />

<strong>Visual</strong> <strong>Basic</strong> does not complain, but simply makes the conversion for you. However,<br />

errors can arise from omitting Val <strong>and</strong> Str. For instance, if the contents of<br />

txtBox1.Text is 34 <strong>and</strong> the contents of txtBox2.Text is 56, then the statement<br />

numVar = txtBox1.Text + txtBox2.Text<br />

assigns the number 3456 rather than 90 to numVar. (This is because <strong>Visual</strong><br />

<strong>Basic</strong> does not perform the conversion until just before the assignment.) If<br />

txtBox1 is empty, then the statement<br />

3 * txtBox1.Text<br />

will stop the program <strong>and</strong> produce the error message “Type mismatch.” We follow<br />

the st<strong>and</strong>ards of good programming practice by always using Val <strong>and</strong> Str to<br />

convert values between text boxes <strong>and</strong> numeric variables. Similar considerations<br />

apply to conversions involving label captions.

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

Saved successfully!

Ooh no, something went wrong!