So Whats an Interface

Having already mentioned the class interface, it may be worthwhile digressing a little to offer an explanation before we proceed with property procedures.

An interface is simply a set of properties and procedures that are exposed to the VBA environment so that code outside the class can access them. We do this by declaring them as Public or Private, much like you would do with any VBA procedure, and by defining their read/write attributes, as shown in Figure 12-6.

Figure 12-6

In the example shown in the preceding section, our interface is defined by those methods and procedures declared as Public. Code outside the class cannot see private members, and therefore, cannot execute them. Properties and methods declared as Private can only be executed from inside the class, and therefore, do not form part of our class's interface.

In the following example, the PrintPayrise() procedure is part of the object's interface, while GivePayrise() is not.

Public Sub PrintPayrise()

'Public methods are part

of the object's interface.

End Sub

Private Sub GivePayrise()

'Private methods are not

part of the object's interface.

End Sub

When creating classes, it is important that you maintain the integrity of its interface. That is, you should avoid changing the property or method names, the names of any arguments, the number of arguments, or their data types. Programmers take a great deal of trouble to write VBA code to instantiate and use a class object that has a specific interface; so if you change that interface, you break the very thing that VBA code needs to make it all work.

On large software projects, where there are many developers working on different parts of the system, a single changed interface can result in many weeks of lost time while everyone changes their code. Rarely does this make for a happy team.

The rule in most software development houses is "never break an interface!" If you need to make changes that will result in the need to change large sections of VBA code, either create a new class or add new methods to the existing one. Existing code will continue to use the existing interface, while newer code that needs to take advantage of any new or modified functionality can use the new ones.

0 0

Post a comment