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.

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

SOLUTION:<br />

We will create a new class, named CPFStudent, with the same member variables <strong>and</strong> property procedures<br />

as the class CStudent. The only change needed in the class module occurs in the SemGrade method. The<br />

new code for this method is<br />

Public Function SemGrade() As String<br />

Dim grade As Single<br />

grade = (m_midterm + m_final) / 2<br />

grade = Round(grade) ‘Round the grade<br />

If grade = 60 Then<br />

SemGrade = “Pass”<br />

Else<br />

SemGrade = “Fail”<br />

End If<br />

End Function<br />

The only change needed in the Form code is to replace the two occurrences of CStudent<br />

with CPFStudent. When the program is run with the same input as in Example 1, the output<br />

will be<br />

Adams, Al 123-45-6789 Pass<br />

■ THE INITIALIZE EVENT PROCEDURE<br />

The Object drop-down combo box in a class module window displays two items General <strong>and</strong><br />

Class. When you click on Class, the following template appears:<br />

Private Sub Class_Initialize()<br />

End Sub<br />

This event procedure is automatically invoked when an object is created from the class. Any<br />

code you type into the procedure is then executed. This procedure is used to set default values<br />

for member variables <strong>and</strong> to create other objects associated with this object.<br />

Since methods are created with ordinary Function or Sub procedures, arguments can be<br />

passed to them when they are called. The graphical program in Example 3 makes use of arguments.<br />

The program involves “twips,” which are a unit of screen measurement. (One inch is<br />

about 1440 twips.) The settings for the Top, Left, Height, <strong>and</strong> Width properties of a control<br />

are given in twips. For instance, the statements<br />

Image1.Width = 1440 ‘1 inch<br />

Image1.Height = 2160 ‘1.5 inch<br />

Image1.Top = 2880 ‘2 inches<br />

Image1.Left = 7200 ‘5 inches<br />

set the size of the image control as 1” by 1.5”, <strong>and</strong> place the control 2 inches from the top of<br />

the form <strong>and</strong> 5 inches from the left side of the form. (See Figure 6-6 in Section 6.3.) Programs<br />

using the Height property of the form should have the BorderStyle property of the form set to<br />

“0-None” since otherwise the height of the border is included in the height of the form.<br />

EXAMPLE 3<br />

Write a program containing a Circle object. The object should keep track of the center <strong>and</strong> radius of the<br />

circle. (The center is specified by two numbers, called the coordinates, giving the distance from the left<br />

side <strong>and</strong> top of the form. Distances <strong>and</strong> the radius should be measured in twips.) A Show method should<br />

display the circle on the form <strong>and</strong> a Move method should add 500 twips to each coordinate of the center<br />

of the circle. Initially, the circle should have its center at (500, 500) <strong>and</strong> radius 500. The form should have<br />

a comm<strong>and</strong> button captioned “Move <strong>and</strong> Show Circle” that invokes both methods.

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

Saved successfully!

Ooh no, something went wrong!