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.

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

10. In the same manner as in Steps 7–9, create the following pair of property procedures<br />

that will be used to retrieve <strong>and</strong> assign values to the variable m_ssn.<br />

Public Property Get SocSecNum() As String<br />

SocSecNum = m_ssn<br />

End Property<br />

Public Property Let SocSecNum(ByVal vNum As String)<br />

m_ssn = vNum<br />

End Property<br />

11. Property procedures can be typed directly into the class module without the use<br />

of Add Procedure. Also, property procedures needn’t come in pairs. For<br />

instance, if we wanted the value of a member variable to be “write only,” we<br />

would use a Property Let procedure <strong>and</strong> have no Property Get procedure. Type<br />

the following two property procedures into the class module. The inclusion of<br />

the word Public is optional.<br />

Property Let midGrade(ByVal vGrade As Single)<br />

m_midterm = vGrade<br />

End Property<br />

Property Let finGrade(ByVal vGrade As Single)<br />

m_final = vGrade<br />

End Property<br />

12. Create the following ordinary Public function with the name SemGrade.<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 />

Select Case grade<br />

Case Is = 90<br />

SemGrade = “A”<br />

Case Is = 80<br />

SemGrade = “B”<br />

Case Is = 70<br />

SemGrade = “C”<br />

Case Is = 60<br />

SemGrade = “D”<br />

Case Else<br />

SemGrade = “F”<br />

End Select<br />

End Function<br />

(This function will be used by our program to invoke a method requesting an<br />

object to calculate a student’s semester grade.)<br />

13. From the File menu, click on Save CStudent As <strong>and</strong> save the class module with<br />

the name 13-1-1S.cls. (We chose this name since the class will be used in<br />

Example 1. Another good choice of name would have been Student.cls. The<br />

extension cls is normally given to files holding class modules.)<br />

14. Click on the form window to activate it. We can now write a program that creates<br />

an object, call it pupil, that is an instance of the class <strong>and</strong> uses the object<br />

to calculate a student’s semester grade. The object variable is declared (in the<br />

general declarations section of the code module) with the statement<br />

Private pupil As CStudent

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

Saved successfully!

Ooh no, something went wrong!