Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do some classes have an "I" in front of their name?

Tags:

vba

I'm working with some legacy code in Visual Basic 98, and there are several classes with an "I" in front of their name. Most of the classes don't have this name, however.

Here's the contents of the IXMLSerializable.cls file.

' Serialization XML:
'       Create and receive complete split data through XML node
Public Property Let SerializationXML(ByVal p_sXML As String)
End Property
Public Property Get SerializationXML() As String
End Property

Public Property Get SerializationXMLElement() As IXMLDOMElement
End Property
like image 255
Joshua McVey Avatar asked Jun 05 '19 13:06

Joshua McVey


2 Answers

Note that VBA supports interfaces, just as C#/VB.NET do (almost). Interfaces are the only way to provide inheritance mechanisms in VBA.

By convention interfaces start their name with the capital letter I.

Here is an example interface declaration that states an object must define a name property

[File: IHasName.cls, Instancing: PublicNotCreatable] 
Option Explicit

Public Property Get Name() As String
End Property

As you can see there is no implementation required.

Now to create an object that uses the interface to advertise that it contains a name property. Of course, the point is that there are multiple classes that use the one interface.

[File: Person.cls, Instancing: Private]
Option Explicit
Implements IHasName

Private m_name As String

Private Sub Class_Initialize()
    m_name = "<Empty>"
End Sub

' Local property
Public Property Get Name() as String
    Name = m_name
End Property

Public Property Let Name(ByVal x As String)
    m_name = x
End Property

' This is the interface implementation that relies on local the property `Name` 
Private Property Get IHasName_Name() As String
    IHasName_Name = Name
End Property

As a convenience in the UI once you include the Implements statement you can choose the interface properties from the top

scr

And to consume the above code use the following test, which calls a function that can take any object that implements IHasName.

[File: Module1.bas]
Option Explicit

Public Sub TestInterface()

    Dim target As New Person
    target.Name = "John"

    GenReport target
    ' This prints the name "John".
End Sub

Public Function GenReport(ByVal obj As IHasName)
    Debug.Print obj.Name
End Function
like image 188
John Alexiou Avatar answered Nov 15 '22 07:11

John Alexiou


The I stands for Interface, like specified in the Microsoft Official Documentation:

IXMLDOMElement Members.

The following tables show the properties, methods, and events.

In C++, this interface inherits from IXMLDOMNode.

That was a pretty common convention and by doing so, you immediately know that it represent an Interface, without looking at the code.

Hope this helps.

like image 41
Louis Avatar answered Nov 15 '22 07:11

Louis