Share via


Interface Basics

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Fundamentally, implementing an interface requires two components. One component defines the interface, and the other implements it.

The component that defines the interface can come from a variety of places. It can be a class module in your project or in a referenced project. It can be a referenced component, such as a DLL. It can even be a built-in class. You can implement any interface contained in a type library, as long as that interface supports Automation. The component that implements the interface is a class module in a Microsoft® Visual Basic® for Applications (VBA) project.

This section assumes that you are designing your own interface, and that you are creating it in a class module in the same VBA project that contains the classes in which you are implementing it. The examples shown here are designed in this way: One class module in the project defines the interface, and one or more class modules implement it.

Note that if the component that provides the interface is not part of your project, you must set a reference to its type library before you can implement it within a class in the project.

To notify VBA that you want to implement an interface, you use the Implements keyword, followed by the name of the interface (which is simply the name of the class that defines the interface). To implement the LibraryItem interface in another class named Periodical, you would type the following line in the Declarations section of the Periodical class:

Implements LibraryItem

After you enter this line, when you click the Object drop-down list in the module's Code window, you'll see that LibraryItem appears in the list of available objects, and all the properties and methods defined for the interface appear in the Procedure drop-down list. For example, if the LibraryItem class includes a property named CheckedOut, the Property Let and Property Get procedures for the CheckedOut property appear in the list. When you select the Property Get procedure from the list, a procedure stub, such as the following one, is added to the class module:

Private Property Get LibraryItem_CheckedOut() As Boolean

End Property

Note that the name of the interface you have implemented precedes the name of the property itself. In addition, note that the procedure stub created is denoted as private. This means that the LibraryItem_CheckedOut property does not form part of the Periodical object's own interface; it will not appear in the list of available properties for an object of type Periodical. Instead, the CheckedOut property will appear in the list of available properties for an object of type LibraryItem.

An important thing to understand when you implement an interface is that an interface is similar to a contract. When you implement an interface in a class module, you agree to include all of the interface's public members in the class module. Every procedure that appears in the Procedure list must be included in the class module before your project will compile, and each procedure must contain either code or a comment. For example, the LibraryItem interface has four read-write properties: Name, AllowCheckOut, ItemType, and CheckedOut. Each class that implements the LibraryItem interface must include the Property Let and Property Get procedures for each of these four properties.

Even if you do not require a particular procedure in your class module, you still have to include it to implement the interface. If you omit a procedure that's provided by the interface, the VBA project will not compile.

Only those members of the interface that are public are visible to the class module that's implementing the interface. In other words, only the public members of the interface are part of the contract, and only they can be implemented by other class modules. The class that describes the interface can include private members, but these members will not be part of the interface.

See Also

Why Build Your Own Objects? | Basic Class Concepts | Creating Property Procedures | Creating Events and Event Procedures | Extending Objects Through Interfaces | Designing Object Models | Creating Custom Objects for Web Pages