The Implements keyword tells VBA that you want to implement an interface class.
An interface class defines a set of declarations (properties and methods) which have to be implemented in a class.
The Implements keyword can only be used in a class modules.
Before we can use the Implements keyword we need to create an Interface class
Fields - They can be read or set directly. Often referred to as variables.
Properties - These are fields but with more control. This is the preferred approach.
Methods - These are actions or tasks you want to perform. They define the behaviour of the object. Also called accessors, operations or functions.
What is an Interface Class?
An interface is like a contract.
When you implement an interface class you agree to include all the interface's public properties and methods in that class module.
Add a class module to your workbook and rename it to "InterfaceClass".
You cannot include an underscore in any of the property or method names.
A property must be implemented in your class even if you declare a field in your interface class.
Public Field1 As Integer
Public Property Get Property1() As String
Public Property Let Property1(ByVal sValue As String)
Public Sub Method1()
Public Function Method2() As String
Implements [InterfaceName | ClassName]
This requires the name of an interface or the name of a class in a type library, whose public variables or methods need to be implemented.
Add another class module to your workbook and rename it to "MyInstance"
Every method and property that appears in the Procedure List must be included in your class module before your project will compile.
Your class module must contain two corresponding property declarations
The name of these property declarations must be prefixed with the name of the interface class.
This prefix is often referred to as implementation interface name prefix
If the type of your public variable is object (or a named class) then you must include property get and property set declarations
If the type of your public variable is variant then you must include property get, property let and property set declarations
Public Property Get InterfaceClass_Field1() As Integer
Debug.Print "Returning value of Field1"
Public Property Let InterfaceClass_Field1(ByVal iValue As Integer)
Debug.Print "Writing value for Field1"
Public Property Get InterfaceClass_Property1() As String
Debug.Print "Returning value of Property1"
Public Property Let InterfaceClass_Property1(ByVal sValue As String)
Debug.Print "Writing value for Property1"
Public Sub InterfaceClass_Method1()
Debug.Print "Method1 - Executing"
Public Function InterfaceClass_Method2() As String
InterfaceClass_Method2 = "Method2 - Returning"
Add a standard code module to your workbook
Public Sub Testing()
Dim myobject As MyInstance
Set myobject = New MyInstance
myobject.InterfaceClass_Field1 = 20
myobject.InterfaceClass_Property1 = "Monday"
VBA does not implement derived class, it exposes the methods that need to be implemented.
VBA does not implement interfaces, it exposes the methods that need to be implemented.
© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext