vba interfaces and properties code example

Example: vba interface

' Simple VBA Interface example
' Add class IMyClass with:
Option Explicit
Public Property Get Name() As String
	' No code
End Property
Public Property Let Name(pName As String)
End Property
Public Function ToString() As String
End Function

' Add class MyClass with:
Option Explicit
Implements IMyClass
Private mName As String
Public Property Get IMyClass_Name() As String
    IMyClass_Name = mName
End Property
Public Property Let IMyClass_Name(pName As String)
    mName = pName
End Property
Public Function IMyClass_ToString() As String
    IMyClass_ToString = "My name is " & mName
End Function

' Test in a module:
Sub TestMe()
    Dim c As New MyClass
    c.IMyClass_Name = "James B"
    Debug.Print c.IMyClass_ToString
End Sub

Tags:

Vb Example