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.

■ FORM-LEVEL VARIABLES<br />

<strong>Visual</strong> <strong>Basic</strong> provides a way to make a variable visible to every procedure in a form’s code without<br />

being passed. Such a variable is called a form-level variable. Form-level variables appear<br />

at the top of the code window <strong>and</strong> are separated from the rest of the code by a horizontal separator<br />

line. Inside the code window, you can move to them either by pressing Ctrl+Home or<br />

clicking on General in the Object list box. Form-level variables are said to reside in the<br />

(Declarations) section of (General) <strong>and</strong> are declared with the following steps.<br />

1. Invoke a code window if one is not already active.<br />

2. Click on the down-arrow to the right of the Object list box.<br />

3. Click on (General).<br />

4. Click on (Declarations) in the Procedure list box.<br />

5. Type in a declaration statement, such as Dim strVar As String, <strong>and</strong> press the<br />

Enter key.<br />

When a form-level variable is assigned a value by a procedure, it retains that value when<br />

the procedure is exited. In this text, we rarely use form-level variables until Section 6.<br />

EXAMPLE 6<br />

The following program contains the form-level variables num1 <strong>and</strong> num2. Their Dim statement does not<br />

appear inside a procedure.<br />

Dim num1 As Single, num2 As Single ‘In (Declarations) section of (General)<br />

Private Sub cmdDisplay_Click()<br />

‘Display the sum of two numbers<br />

num1 = 2<br />

num2 = 3<br />

picResults.Cls<br />

Call AddAndIncrement<br />

picResults.Print<br />

picResults.Print “num1 = “; num1<br />

picResults.Print “num2 = “; num2<br />

End Sub<br />

Private Sub AddAndIncrement()<br />

‘Display numbers <strong>and</strong> their sum<br />

picResults.Print “The sum of”; num1; “<strong>and</strong>”; num2; “is”; num1 + num2<br />

num1 = num1 + 1<br />

num2 = num2 + 1<br />

End Sub<br />

[Run, <strong>and</strong> click the comm<strong>and</strong> button. The following is displayed in the picture box.]<br />

The sum of 2 <strong>and</strong> 3 is 5<br />

num1 = 3<br />

num2 = 4<br />

Sub Procedures, Part II 85<br />

In the preceding example, we had to click a comm<strong>and</strong> button to assign values to the<br />

form-level variables. In some situations, we want to assign a value immediately to a formlevel<br />

variable, without requiring the user to perform some specific action. <strong>Visual</strong> <strong>Basic</strong> has<br />

a special event procedure called Form_Load that is automatically activated as soon as the<br />

program is run, even before the form is created. The Form_Load template is invoked by double-clicking<br />

on the form itself.

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

Saved successfully!

Ooh no, something went wrong!