*

Aggregation Mini-Lecture: Day 4

When we last left you, our intrepid programmer, you were slightly freaking out about how to maintain your code.  After a meeting, the vet was cool for now with leaving the attention advice at "will need to be petted a lot and made a fuss over", but still wanted to add a category for vet visitations with the advice, "will need to get shots and checked for parasites", and wanted to modify the attention advice after the next veterinary conference.  There were no new categories of pets to add, but the vet wasn't ruling out adding some in the future.  So, for right now, your main concern was how to make sure that any changes to the two common types of pet care, Attentions and VetVisitations,  were implemented in all the individual pet classes.  "Wait...that word rings a bell...Implemented...Implement...IMPLEMENTS! That's it! That's how I can do 'includes'!"

We'll start with bringing back the clsPetCare (link opens new window) class module, but it will only have the two common properties, Attentions and VetVisitations.  These are read-only properties so we'll only need to code the Property Get.

Cool!  Now, to do the include part on those individual modules...we'll start with clsCat, and put this code up in the General Declarations area:

'to "include" clsPetCare's Attention and VetVisitation properties
Implements clsPetCare
Private objPetCare As clsPetCare

There's that Implements keyword that gives us access to clsPetCare's public properties.  And we'll also need a module-level variable to represent our instantiation of clsPetCare. 

Now, we need a place to actually instantiate objPetCare - how about the Initialize event?!  And, of course, we need to make sure we do the IUnknown::Release, so we'll put some de-instantiation code in the Terminate event:

Private Sub Class_Initialize()
    Set objPetCare = New clsPetCare
End Sub

Private Sub Class_Terminate()
    Set objPetCare = Nothing
End Sub

All right, now that we have our included object ready to use, what do we do with it now?

Hmmm.  This is the tricky part - part of the rules of using Implements is that we have to make sure we refer to all the public properties (and/or methods).  And there was something else...let's check the documentation:

When a Visual Basic class implements an interface, the Visual Basic class provides its own versions of all the Public procedures specified in the type library of the Interface.

Ah.  Well, let's take care of the first part.  In the Object drop list we see a listing for clsPetCare (this is because of the Implements) and in the Procedure drop list we see listings for Attention and VetVisitations.  Let's pull down those procedure stubs so we can put some code in there....just like a regular property we make the property name equal to...oh, that's nifty! look! here's where we use the instance of clsPetCare we created - we make it call its properties!

Private Property Get clsPetCare_Attentions() As String
   clsPetCare_Attentions = objPetCare.Attentions
End Property
Private Property Get clsPetCare_VetVisitations() As String
    clsPetCare_VetVisitations = objPetCare.VetVisitations
End Property

Wait...whoa! Those are Private procedures - how is the form going to be able to see them??  Oh, yeah, that's the part from the documentation about providing "its own versions".  OK, how about this:

Public Property Get Attentions()
    Attentions = clsPetCare_Attentions
End Property

Public Property Get VetVisitations()
    VetVisitations = clsPetCare_VetVisitations
End Property

We'll just make the public procedure refer back to the private procedure.  Cool! 

Because all that code is really generic and doesn't specifically mention clsCat, we can just copy and paste it into clsDog, clsRodent and clsReptile! 

Now, if the vet needs the common advice changed, we just need to change it in clsPet and the other classes that implement clsPet will automatically know! And what's really great, too, is that we don't have to change any of the code in the form!

Well, that's solved now.  It was a bummer about having to get rid of the PetCareAdvised event, but with the late binding/as object thing, there was no way we could use the event, and, really, it was just for resetting the values anyhow...

So goes you, the intrepid programmer, off for a well-deserved nap, even though the event thing bothers you...

That night, you awake in a cold sweat after dreaming about zoos and aquariums and other places that have many, many sorts of animals.  How many class modules can you have?  Yeah, you solved the problem of coding the common information, but wow that's a lot of code...There has to be a more efficient way of coding this...

Download the Code*



Internet Content Rating Association

Alrak's Course Resources ©2002-2007 Karla Carter. All rights reserved. This material (including, but not limited to, Mini-lectures and Challenge Labs) may not be reproduced, displayed, modified or distributed without the express prior written permission of the copyright holder. For permission, contact Karla.