Search notes:

VBA: Class interfaces

The main purpose of interfaces is to guarantee that classes that implement the interface provide a set of functions and subs.
Thus, an interface is somewhat similar to a pure abstract class in C++

Iexample

An interface is an ordinary class file that contains functions and subs.
Within such file, there is no language construct (such as a interface keyword) that indicates that this is an interface.
Because the following class is used as an interface, the bodies of the subs and functions are left empty.
option explicit

public sub      aSub()                 : end sub
public function aFunc(param as double) : end function
Github repository about-VBA, path: /language/interfaces/example/Iexample.cls

Implementing the interface

The following two classes (named Foo and Bar) implement the interface.
The source code requires the keyword implements, followed by the name of the interface that a class wants to implement.
A class that implements an interface is required to provide a sub or function for all public subs and functions that are defined in the interface class. If one of these functions/subs is not provided, the compiler will issue the error message Object module needs to implement «functionName» for interface «interfaceName».
Thus, using interfaces guarantees that a set of subs and functions along with certain parameters are implemented by the class which then can be used by the class's consumer.
The methods that are declared in the interface must have a prefix with the name of the interface followed an underscore and the sub/function that is implemented
In this example, these names are IExample_aSub and IExample_aFunc.

Foo.cls

option explicit

implements Iexample

public sub Iexample_aSub()
    msgBox "bar says hello"
end sub

public function Iexample_aFunc(param as double)
    Iexample_aFunc = param*5
end function

Bar.cls

option explicit

implements Iexample

public sub Iexample_aSub()
    msgBox "foo says hello"
end sub

public function Iexample_aFunc(param as double)
    Iexample_aFunc = param*2
end function

Using the interfaces

When using interfaces, the variables that store an object that implements an interface can be declared with the name name of the interface: dim ex_1 as Iexample and assigned a new implementation of that interface with set ex_1 = new Foo.
option explicit

sub runExample()

    dim ex_1 as Iexample
    dim ex_2 as Iexample

    set ex_1 = new Foo
    set ex_2 = new Bar

    ex_1.aSub
    ex_2.aSub

    debug.print ex_1.aFunc(11)
    debug.print ex_2.aFunc(11)

end sub
Github repository about-VBA, path: /language/interfaces/example/main.vb

Creating the example

An Excel workbook that contains the source files for this example can be created with the VBScript MS-Office App Creator and the following script:
<job>
<script language="VBScript" src="..\..\..\VBS-MS-Office-App-Creator\create-MS-Office-app.vbs" />
<script language="VBScript">

   option explicit

   dim app
   dim xls

   set xls = createOfficeApp("excel", currentDir() & "created.xlsm")
   if xls is nothing then ' {
      wscript.echo("Could not create excel worksheet.")
      wscript.quit(-1)
   end if ' }

   set app = xls.application

   insertModule app, currentDir() & "Iexample.cls", "Iexample", 2
   insertModule app, currentDir() & "Foo.cls"     , "Foo"     , 2
   insertModule app, currentDir() & "Bar.cls"     , "Bar"     , 2
   insertModule app, currentDir() & "main.vb"     , "example" , 1

   app.run "runExample"

   xls.save

   createObject("WScript.Shell").appActivate(app.caption)

</script> </job>
Github repository about-VBA, path: /language/interfaces/example/createExcel.wsf

See also

VBA classes
VBA language

Links

Vitosh: VBA – Interfaces in VBA How And Why? and the corresponding github repository.

Index