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.

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

EXAMPLE 4<br />

The following program uses Sub procedures to perform the same tasks as the program in Example 3.<br />

‘In BAS Module<br />

Public Type collegeData<br />

nom As String * 30<br />

state As String * 2<br />

yearFounded As Integer<br />

End Type<br />

‘In Form code<br />

Private Sub cmdProcess_Click()<br />

‘Demonstrate use of records<br />

picBox.Cls<br />

Dim college As collegeData<br />

Call GetDat(college)<br />

Call DisplayStatement(college)<br />

End Sub<br />

Private Sub DisplayStatement(school As collegeData)<br />

Dim century As Integer, when As String<br />

century = 1 + Int(school.yearFounded / 100)<br />

picBox.Print RTrim(school.nom); “ was founded in the” & Str(century);<br />

picBox.Print “th century in ”; school.state<br />

Dim university As collegeData<br />

university.nom = “M.I.T.”<br />

university.state = “MA”<br />

university.yearFounded = 1878<br />

If school.yearFounded < university.yearFounded Then<br />

when = “before ”<br />

Else<br />

when = “after ”<br />

End If<br />

picBox.Print RTrim(school.nom); “ was founded ”;<br />

picBox.Print when; RTrim(university.nom)<br />

End Sub<br />

Private Sub GetDat(school As collegeData)<br />

school.nom = txtCollege.Text<br />

school.state = txtState.Text<br />

school.yearFounded = Val(txtYear.Text)<br />

End Sub<br />

COMMENTS<br />

1. Record variables are similar to arrays in that they both store <strong>and</strong> access data items<br />

using a common name. However, the elements in an array must be of the same<br />

data type, whereas the fields in a record variable can be a mixture of different data<br />

types. Also, the different elements of an array are identified by their indices,<br />

whereas the fields of a record are identified by a name following a period.<br />

2. If the record variables recVar1 <strong>and</strong> recVar2 have the same type, then all the field<br />

values of recVar2 can be assigned simultaneously to recVar1 by the statement<br />

recVar1 = recVar2

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

Saved successfully!

Ooh no, something went wrong!