• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 13. Exploiting the Power of Clas... > Creating Multiple Class Instances

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


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint