Defining Automation Support

[Previous] [Next]

You can define all interfaces, with the exception of the default interface, in a COM IDL file and compile the file into a type library using the MIDL compiler. Visual Basic doesn't allow you to change the default interface. The default interface is a dual interface, meaning it's a custom interface that derives from IDispatch. IDispatch is the only interface that a scripting language client can reference. For example, this type of reference occurs in VBScript when a variable is declared as Object, which is illustrated in the following code fragment.

 Dim d As Object Set d = CreateObject("CanineSvr.DogImpl") d.bark 

The call to the bark method translates to a call to the IDispatch.Invoke method, which invokes the default interface bark method call. If the bark method is defined in another interface implemented by the DogImpl class, Visual Basic will raise run-time error 438: "Object doesn't support this property or method." The reason this error is raised is that because the scripting language doesn't support custom interface referencing, it has no means by which to query for an interface. To address this problem, Visual Basic defines a default dual interface that the scripting language can reference. According to the rules of Automation implemented in Visual Basic, IDispatch can invoke methods only on its custom interface counterpart. This limitation can be overcome in other languages, such as C++ and Visual J++, but unfortunately not in Visual Basic. Hence, most Visual Basic programmers consider dual interfaces evil.

Nonetheless, with a conscious architecture decision, you can make dual interfaces work for you in Visual Basic to provide scripting support for all interfaces implemented in a Visual Basic COM class. This support can be accomplished by using the default interface only to define interface accessor methods for all other dual interfaces implemented by the class. The following code fragment is an example of such an implementation.

 ' Class FooBar defined in project FBLib Option Explicit Implements IFoo Implements IBar ' Define interface accessor methods in the implicit default ' interface FooBar in class FooBar to access interfaces IFoo ' and IBar. Public Function getIFoo() As IFoo Set getIFoo = Me End Function Public Function getIBar() As IBar Set getIBar = Me End Function ' IBar interface implementation Private Sub IBar_doBar() MsgBox "Doing IBar" End Sub ' IFoo interface implementation Private Sub IFoo_doFoo() MsgBox "Doing IFoo" End Sub Private Function IFoo_doFoo2(A As Integer, B As Integer) As Integer IFoo_doFoo2 = A * B End Function 

A scripting language client will now be able to access the nondefault interfaces IFoo and IBar because of the interface accessor methods defined in the default FooBar interface and because IFoo and IBar are also dual interfaces. The following code fragment illustrates a scripting client accessing the IFoo and IBar interfaces.

 ' Visual Basic Scripting Client Dim myFoo As Object Dim myBar As Object Dim myFooBar As Object Dim iAnswer As Integer Set myFooBar = CreateObject("FBLib.FooBar") Set myFoo = myFooBar.getIFoo() myFoo.doFoo iAnswer = myFoo.doFoo2(5, 2) Set myBar = myFooBar.getBar() myBar.doBar 



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