Defining Interfaces in Visual Basic
What
keeps us from upgrading the CAccount class is that the client has a
dependency on the first version of the public members of the class.
There is no problem with changing private members, since
they’re completely transparent to the client, but you cannot
change public members. There is a one-to-one relationship between the
class itself and the public members of the class. If the public
members were not part of the CAccount class, then the client would
not have dependencies on the class, and the class could be changed
without problems. Therefore, we must dissociate the
public member declarations from
their implementation. To do this, a new entity known as an
interface is built. The simplest definition of
an interface is that it is a class with public properties and methods
but without any code. The class serves as a definition of methods, a
protocol that a client will use to communicate with the class. In
Chapter 3, you are going to learn a more concrete
definition. However, for now it is sufficient to think of an
interface as a class with declarations but without code. For example,
let’s return to the CAccount class. Instead of adding public
members directly to this class, let’s create a second class and
call it IAccount
. The IAccount
class will have the following code:
' Class IAccount Public Property Get Balance( ) As Currency End Property Public Sub MakeDeposit(ByVal Amount As Currency) End Sub
As you can see from the definition, there are ...
Get COM+ Programming with Visual Basic now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.