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.

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

■ COLLECTIONS<br />

A collection of objects is an ordered set of objects where the objects are identified by the<br />

numbers 1, 2, 3, . . . A collection is declared with a statement of the form<br />

Dim collectionName As New Collection<br />

<strong>and</strong> initially contains no objects. (We say that the variable collectionName has type Collection.)<br />

The statement<br />

collectionName.Add objectName<br />

adds the named object to the collection <strong>and</strong> automatically assigns it the next available number.<br />

The numbers for the different objects will reflect the order they were added to the collection.<br />

The statement<br />

collectionName.Remove n<br />

deletes the nth object from the collection <strong>and</strong> automatically reduces the object numbers from<br />

n +1 on by 1 so that there will be no gap in the numbers. At any time, the value of<br />

collectionName.Count<br />

is the number of objects in the collection. The value of<br />

collectionName.Item(n).propertyName<br />

is the value of the named property in the nth object of the collection. The statement<br />

collectionName.Item(n).methodName<br />

runs the named method of the nth object of the collection.<br />

EXAMPLE 1<br />

In the following program, the user enters four pieces of data about a student into text boxes <strong>and</strong> selects a<br />

type of registration. When the AddStudent button is pressed, the data is used to create <strong>and</strong> initialize an<br />

appropriate object (either from class CStudent or class CPFStudent) <strong>and</strong> the object is added to a collection.<br />

When the Calculate Grades button is pressed, the name, social security number, <strong>and</strong> semester grade<br />

for each student in the collection is displayed in the picture box.<br />

Object Property Setting<br />

frm13_2_1 Caption Semester Grades<br />

lblName Caption Name<br />

txtName Text (blank)<br />

lblSSN Caption SSN<br />

txtSSN Text (blank)<br />

lblMidterm Caption Midterm<br />

txtMidterm Text (blank)<br />

lblFinal Caption Final<br />

txtFinal Text (blank)<br />

fraType Caption Type of<br />

Registration<br />

optReg Caption Regular<br />

optP Caption Pass/Fail<br />

cmdAdd Caption &Add Student<br />

cmdSemGrade Caption &Calculate Grades<br />

cmdQuit Caption &Quit<br />

picGrades

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

Saved successfully!

Ooh no, something went wrong!