1

I'm having a hard time understanding and working with Implements and I'm failing to see why this is of any use if Inheritance isn't supported with VBA.

I'm testing the code below and I keep getting the error:

Compile Error:

Object module needs to implement '~' for interface '~'

Interface: ITransmission

Option Explicit

Public pVENDOR As String

Public Property Get VENDOR() As String
End Property
Public Property Let VENDOR(ByVal value As String)
End Property

Base Class: cASN

Option Explicit
Implements ITransmission

Private Property Let ITransmission_pVENDOR(ByVal value As String)
   pVENDOR = value
End Property

Private Property Get ITransmission_pVENDOR() As String
   ITransmission_pVENDOR = pVENDOR
End Property

Unit Test Method: mUnitTesting

Private Sub Test_cASN()

   Dim foo As cASN

   Set foo = New cASN

   foo.VENDOR = "Test"

End Sub

Still very new to Implements and it is something I want to learn, and I've done a fair amount of research into it.

Question 1:

Why am I getting an error message when I try to unit test this?

Question 2:

What is the real benefit here, if inheritance isn't supported?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Maldred
  • 1,074
  • 4
  • 11
  • 33
  • Possible duplicate of [How to use the Implements in Excel VBA](https://stackoverflow.com/questions/19373081/how-to-use-the-implements-in-excel-vba) – Sorceri Sep 11 '18 at 16:14

1 Answers1

1

You implement pVENDOR but not the two VENDOR properties.

I'm assuming you want the interface to be a get/let of the VENDOR property.

Your Public pVENDOR As String looks like a backing field for this property, as an Interface cannot include an implementation then its not needed.

The Interface should look like:

Public Property Get VENDOR() As String
End Property

Public Property Let VENDOR(ByVal value As String)
End Property

Then when you implement it:

Implements ITransmission

Private pVENDOR As String '// local implementation detail

Public Property Let ITransmission_VENDOR(ByVal value As String)
    pVENDOR = value
End Property

Public Property Get ITransmission_VENDOR() As String
    ITransmission_VENDOR = pVENDOR
End Property

And to test:

Private Sub Test_cASN()

   Dim foo As cASN

   Set foo = New cASN

   foo.ITransmission_VENDOR = "Test"

End Sub

What is the real benefit here

How will I know when to create an interface?

The point of an Interface

Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • This is great thanks! I found this as well... Is this an optimal work around to inheritance as well? https://www.microsoftpressstore.com/articles/article.aspx?p=2225071&seqNum=2 – Maldred Sep 11 '18 at 17:03
  • Also, to make this clear... to my understanding an `Interface` is essentially a template that a class would have to follow? So, if I had two unique classes that implement the `ITransmission` interface, these two classes **MUST** include these Get/Let properties, which is essentially what I'm trying to accomplish. – Maldred Sep 11 '18 at 17:06
  • yes... the must implement all the methods of the interface class. – QHarr Sep 11 '18 at 20:52