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.

Task Statement<br />

Assign a value to a property objectName.propertyName = value<br />

Display the value of a property picBox.Print objectName.propertyName<br />

Carry out a method objectName.methodName(arg1, ...)<br />

Raise an event RaiseEvent eventName<br />

The following walkthrough creates a student class <strong>and</strong> a program that uses that class.<br />

The data stored by an object of this class are name, social security number, <strong>and</strong> grades on<br />

two exams (midterm <strong>and</strong> final).<br />

1. Start a new program.<br />

2. From the Project menu on the toolbar, click on Add Class Module.<br />

3. Double-click on Class Module in the Add Class Module dialog box. (The window<br />

that appears looks like an ordinary code window.)<br />

4. If the Properties window is not visible, press F4 to display it. (Notice that the<br />

class has the default name Class1.)<br />

5. Change the setting of the Name property to CStudent. (We will follow the common<br />

convention of beginning each class name with the uppercase letter C.)<br />

6. Type the following lines into the code module.<br />

Private m_name As String<br />

Private m_ssn As String<br />

Private m_midterm As Single<br />

Private m_final As Single<br />

(These lines of code declare four variables that will be used to hold data. The<br />

word Private guarantees that the variables cannot be accessed directly from outside<br />

the object. In object-oriented programming terminology, these variables<br />

are called member variables (or instance variables). We will follow the common<br />

convention of beginning the name of each member variable with the prefix<br />

“m_”.)<br />

7. From the Tools menu on the toolbar, click on Add Procedure. (As before, an<br />

Add Procedure dialog box will appear.)<br />

8. Type “Name” into the Name text box, click on Property in the Type frame, <strong>and</strong><br />

click on OK. The following lines will appear in the class module window.<br />

Public Property Get Name() As Variant<br />

End Property<br />

Public Property Let Name(ByVal vNewValue As Variant)<br />

End Property<br />

9. Change the words Variant to String, the word vNewValue to vName, <strong>and</strong> type<br />

code into the two property procedures as shown below.<br />

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

Name = m_name<br />

End Property<br />

Public Property Let Name(ByVal vName As String)<br />

m_name = vName<br />

End Property<br />

The first procedure will be called by our program to retrieve the value of the<br />

variable m_name <strong>and</strong> the second procedure will be called to assign a value to<br />

the variable m_name.<br />

Classes <strong>and</strong> Objects 357

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

Saved successfully!

Ooh no, something went wrong!