Defining COM Interfaces and Classes in Visual Basic

[Previous] [Next]

COM classes are named implementations of COM interfaces. A COM class can implement multiple interfaces and must implement at least one. A client obtains a reference to an instance of a COM class (an object) that implements an interface that the client expects.

A public creatable Visual Basic class is a COM class that has, at minimum, a single interface, which is referred to as its default interface. The default interface is provided internally by Visual Basic and serves two main purposes. First, it provides Automation support, which I'll go over in more detail later in this appendix. Second, it simplifies the formality of COM by making the distinction between the default interface and the implementation (the COM class) implicit. This implicit distinction allows class properties and methods defined as Public, such as those in the following code fragment, to automatically become part of the default interface.

 ' Class BankAccount ' Public Property Get Balance() As Double ' Return the bank account balance. End Property Public Function Deposit(Amount As Double) As Double ' Deposit funds in the bank account. End Function 

This implicit use of the default interface also allows a Visual Basic client to declare object variables as types that appear to be class names, not distinguishable interface names, as shown in the following code fragment.

 ' Visual Basic Client ' Dim myBankAccount As BankAccount Dim dblBalance As Double Set myBankAccount = New BankAccount dblBalance = myBankAccount.Deposit(1000) 

To me, declaring variables in this way seems to cloud the COM rule of separating the interface from the implementation and only leads to confusion. But the default interface exists, and myBankAccount is in reality typed as an interface, not as a class.

As illustrated throughout Part II of this book, defining and implementing interfaces in Visual Basic is straightforward. To define an interface, perform the following steps.

  1. Add a class module to an ActiveX project.
  2. Change the class module's Instancing property value to PublicNotCreatable.
  3. Declare public properties and methods with no implementation code, as shown in the following code fragment.
  4.  ' Interface IBankAccount ' Public Property Get Balance() As Double End Property Public Function Deposit(Amount As Double) As Double End Function 

  5. Implement additional interfaces in a Visual Basic class via the keyword Implements, as shown in the following code fragment.

 ' Class BankAccountImpl ' Implements IBankAccount Private Property Get IBankAccount_Balance() As Double ' Return the bank account balance. End Property Private Function IBankAccount_Deposit(Amount As Double) _ As Double ' Deposit funds in the bank account. End Function 

You can see that implementing additional interfaces in a class is explicit. Notice that the implementation functions are declared as Private. Public declarations always apply to the default interface, which in the previous code fragment would be BankAccountImpl. Also, as the next code fragment illustrates, the client must explicitly declare a variable of type IBankAccount to reference the IBankAccount interface.

 ' Visual Basic Client ' Dim myBankAccount As IBankAccount Dim dblBalance As Double Set myBankAccount = New BankAccountImpl dblBalance = MyBankAccount.Deposit(1000) 

Providing that the code has no bugs, when you make the ActiveX project Visual Basic will create a type library that contains COM interface and class definitions as originally defined using only Visual Basic language features. (A type library is a COM artifact that consists of COM interface and class definitions in a binary package.) The type library is embedded into the ActiveX compiled file (DLL, OCX, or EXE). When the ActiveX component is registered on a computer, information about the interfaces and classes contained in the type library are added to the Windows Registry on that computer (more on this subject later).



Microsoft Visual Basic Design Patterns
Microsoft Visual Basic Design Patterns (Microsoft Professional Series)
ISBN: B00006L567
EAN: N/A
Year: 2000
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net