I tried to implement a new class that is implementing two interfaces in Excel VBA but I have compilation problems as the members of the interfaces do not seem to be properly implemented in the implementing class (they are not callable).
The Interfaces are the following:
ICrawlable
:
Option Explicit
Public Function GetCrawler() As ICrawler
End Function
IEquatable
:
Option Explicit
Public Function Equals(CompareObject As Variant) As Boolean
End Function
Whereas ICrawlable
also holds a function returning the interface ICrawler
:
Option Explicit
Public Property Get CurrentItem() As Variant
End Property
Public Sub MoveNext()
End Sub
Public Function GetNext() As Variant
End Function
Public Function ItemsLeft() As Boolean
End Function
I created a sample class InterfaceTester
using those two first interfaces:
Option Explicit
Implements ICrawlable
Implements IEquatable
Private Function ICrawlable_GetCrawler() As Variant
End Function
Private Function IEquatable_Equals(CompareObject As Variant) As Boolean
End Function
When trying to use that class in a Module or wherever else I do not have Intellisense support. Furthermore, the code does not compile and I get a "Method or Data member not found" in this module code highlighting the .Equals
:
Sub TestInterfacing()
Dim TestInstance As InterfaceTester
Set TestInstance = New InterfaceTester
Dim VerificationInstance As InterfaceTester
Set VerificationInstance = New InterfaceTester
Dim Result As Boolean
Result = TestInstance.Equals(VerificationInstance)
End Sub
Is this a bug in VBA? Have I declared things in the interfaces that are not allowed (I have tried changing all return data types to Variant already and have tried disabling each of the functions of the interfaces)? Do I use reserved keywords (in the object explorer I do not see duplicates of my interface names)? Does it compile on your machines?
If you want to use methods from the interface, your variable must be declared as this interface type:
Sub TestInterfacing()
Dim TestInstance As IEquatable 'InterfaceTester
Set TestInstance = New InterfaceTester
Dim VerificationInstance As InterfaceTester
Set VerificationInstance = New InterfaceTester
Dim Result As Boolean
Result = TestInstance.Equals(VerificationInstance)
End Sub
However, in this case you cannot use methods from the second interface implemented by this class: ICrawlable
.
The reason for this is that in VBA implementing method looks like that:
Private Function ICrawlable_GetCrawler() As ICrawler
while using the rules from other languages it should look like below:
Public Function GetCrawler() As ICrawler
VBA would not understand that this is implementation of interface's method GetCrawler
.
In order to overcome this issue you should add another two public methods to InterfaceTester
class - Equals
and GetCrawler
. Implementing methods should only direct to those methods:
InterfaceTester
class:
Implements ICrawlable
Implements IEquatable
Public Function Equals(CompareObject As Variant) As Boolean
'implementation
End Function
Public Function GetCrawler() As ICrawler
'implementation
End Function
Private Function ICrawlable_GetCrawler() As ICrawler
Set IEquatable_Equals = GetCrawler
End Function
Private Function IEquatable_Equals(CompareObject As Variant) As Boolean
IEquatable_Equals = Equals(CompareObject)
End Function
Now you can declare your variable TestInstance
as InterfaceTester
class and use methods from both interfaces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With