Alison Balteramp;#039;s Mastering Microsoft Office Access 1002003 [Electronic resources]

Alison Balter

نسخه متنی -صفحه : 544/ 220
نمايش فراداده

Creating Multiple Class Instances

One of the advantages of Class modules is that you can create multiple instances of the class. Each instance maintains its own variables and executes its own code. This is illustrated in the following code:

Sub MultipleInstance() 'Declare both class objects Dim oPerson1 As Person Dim oPerson2 As Person 'Instantiate both class objects Set oPerson1 = New Person Set oPerson2 = New Person 'Set the first name and last name 'properties of the oPerson1 object oPerson1.FirstName = "Alison" oPerson1.LastName = "Balter" 'Display the return value from the Speak 'method of the first instance in a message box MsgBox oPerson1.Speak 'Set the first name and last name 'properties of the oPerson2 object oPerson2.FirstName = "Dan" oPerson2.LastName = "Balter" 'Display the return value from the Speak 'method of the second instance in a message box MsgBox oPerson2.Speak End Sub

The code creates two instances of the Person class. The first is referred to as oPerson1 and the second as oPerson2. The code sets the FirstName property of oPerson1 to Alison, and the LastName property of oPerson1 to Balter. The code sets the FirstName property of oPerson2 to Dan, and the LastName property of oPerson2 to Balter. The Speak method returns the name of the correct person which the code displays in a message box.