Maldred
Maldred

Reputation: 1104

VBA Implements / Inheritance

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?

Upvotes: 1

Views: 1634

Answers (1)

Alex K.
Alex K.

Reputation: 175768

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

Upvotes: 1

Related Questions