Polymorphic ClassesThe ability of a class to appear to be many different types of object is called polymorphism and is something that many of the classes in Excel's object model use. For example, we can access the different aspects of the various menu item types using their detailed interfacesCommandBarPopUp, CommandBarButton, CommandBarComboBox and so onor iterate through them all using the more generic set of properties that they expose through the CommandBarControl interface. We can make our own classes polymorphic by simply defining and implementing multiple custom interfaces, in the same way that we added the ISortableObject interface.For example, another requirement for our fictional book-publishing application might be to generate a letter for everyone involved in the book's production. Ideally we would like to be able to put all the CAuthor, CReviewer, CEditor and CDistributor objects into a single collection, sort the collection and then loop through it to generate the letters. Adding all the objects into one collection is not a problemthe Collection object can handle mixed object types. Assuming all those classes have implemented our ISortableObject interface, sorting the collection is not an issue eitherour generic sorting routine doesn't care what type of object it's looking at, so long as it implements the interface. The problem comes when we want to generate the lettershow do we iterate through the collection of mixed object types to get the contact details? The answer, of course, is to add another custom interface to those classes, through which we can access the contact details and other properties that are common to all the objects (assuming we've extended the earlier CAuthor class to include those details). We might choose to call it the IContactDetails interface, shown in Listing 11-10, and include the name, postal address and so forth. Listing 11-10. The IContactDetails Interface ClassThe extended CAuthor, CReviewer, CEditor and CDistributor classes can implement that interface, resulting in the CAuthor class looking like Listing 11-11, where the extra code to implement the IContactDetails interface has been highlighted. Listing 11-11. The CAuthor Class Implementing the IContactDetails InterfaceWhen using the interface and procedure name drop-downs to add the Property Let procedures, the VB editor always uses RHS as the variable name for the new property value (because that represents the "right-hand side" of the property assignment expression). If the code will be doing anything other than just passing the value on to another procedure, it is a very good idea to give the variable a more meaningful name, in line with the best practices on naming conventions explained in Chapter 3 Excel and VBA Development Best Practices.After we've added the interface to all our classes, we can add the classes to a single collection, sort the collection using the ISortableObject interface and iterate through it using the IContactDetails interface, shown in Listing 11-12. The ShowDetails procedure processes the contact details for each object in the collection, again using the IContactDetails interface, and is explained later. Listing 11-12. Sorting and Listing Mixed Classes That Implement ISortableObject and IContactDetailsWe can use the TypeOf function to test whether a class implements a certain interface and switch between interfaces by declaring a variable as the type of interface we want to look through, then setting it to refer to the object, as shown in Listing 11-13. Regardless of which interface we're looking through, the VB TypeName() function will always return the object's class name. Listing 11-13. Checking an Object's Interfaces
![]() |