Interfaces in Visual Basic


To create an interface in Visual Basic, you simply create a class module with empty methods and properties. Here’s an IFilter interface (in IFILTER.CLS) that defines the standard members of a filter. IFilter has Source and Target properties to specify the data to be processed. It has a Translate member to do the work. And, in this case, there’s an EChunkAction Enum type that indicates the results of the Translate method:

Enum EChunkAction
ecaAbort
ecaSkip
ecaTranslate
End Enum

Property Get Source() As String
End Property
Property Let Source(sSourceA As String)
End Property

Property Get Target() As String
End Property
Property Let Target(sTargetA As String)
End Property

Function Translate(sChunkA As String, ByVal iChunkA As Long) As EChunkAction
End Function

Nothing to it. Literally. An interface has no code because it doesn’t do anything. You have to create at least one separate class that implements the interface. The interface itself doesn’t care how the members work, or even whether they work.


Since IFilter is an interface class, I use the convention of starting the names of such classes with an uppercase I. IFilter is what you would call an abstract base class in many object-oriented languages. Its purpose is to provide a type that your program can bind to at compile time, even though you won’t know the actual object type until run time.