psyang
Mar 21st, 2001, 10:30 AM
I am working on a project where interfaces are defined in TLBs via IDL, and are implemented in VB using Implements.
Since VB can only implement interfaces derived from IUnknown, it was decided that every interface support a standard set of properties which can be queried by a central error object. Things like the interface name can then be retreived regardless of what interface was passed in - polymorphism!
The error object takes, as a parameter, a reference to the interface. Since this interface can be anything, the type of the parameter is Object.
Unfortunately, when an interface is passed in, the type changes to the implementing class' default interface. As an example:
Class 1
-------
Implements Class2
Property Get Interface() as String
Interface = "Class1"
End Property
Property Get Class2_Interface() as String
Class2_Interface = "Class2"
End Property
Error Object
------------
Public Sub PushError( objInterface as Object )
MsgBox "Error in " & objInterface.Interface
End Sub
Main Tester
-----------
Sub Main()
dim objClass1 as Class1
dim objClass2 as Class2
dim objError as ErrorObject
set objError = new ErrorObject
set objClass1 = New Class1
objError.PushError( objClass1 ) ' Displays "Class1"
set objClass2 = objClass2
objError.PushError( objClass2 ) ' Displays "Class1", not "Class2"
End Sub
What bugs me is that if the interfaces for Class1 and Class2 are defined in VB (ie. a "dummy class" is created with just the interface and no implementation), this works fine. When the interfaces come from a TLB, however, PushError always reports Class1 in my above example.
As well, in the Watch Window, when the class definition is done in VB, objInterface is correctly reported as Object/Class1 or Object/Class2. When the class definition is done in TLBs, objInterface is always reported as Object/Class1.
Any ideas?
-Peter
Since VB can only implement interfaces derived from IUnknown, it was decided that every interface support a standard set of properties which can be queried by a central error object. Things like the interface name can then be retreived regardless of what interface was passed in - polymorphism!
The error object takes, as a parameter, a reference to the interface. Since this interface can be anything, the type of the parameter is Object.
Unfortunately, when an interface is passed in, the type changes to the implementing class' default interface. As an example:
Class 1
-------
Implements Class2
Property Get Interface() as String
Interface = "Class1"
End Property
Property Get Class2_Interface() as String
Class2_Interface = "Class2"
End Property
Error Object
------------
Public Sub PushError( objInterface as Object )
MsgBox "Error in " & objInterface.Interface
End Sub
Main Tester
-----------
Sub Main()
dim objClass1 as Class1
dim objClass2 as Class2
dim objError as ErrorObject
set objError = new ErrorObject
set objClass1 = New Class1
objError.PushError( objClass1 ) ' Displays "Class1"
set objClass2 = objClass2
objError.PushError( objClass2 ) ' Displays "Class1", not "Class2"
End Sub
What bugs me is that if the interfaces for Class1 and Class2 are defined in VB (ie. a "dummy class" is created with just the interface and no implementation), this works fine. When the interfaces come from a TLB, however, PushError always reports Class1 in my above example.
As well, in the Watch Window, when the class definition is done in VB, objInterface is correctly reported as Object/Class1 or Object/Class2. When the class definition is done in TLBs, objInterface is always reported as Object/Class1.
Any ideas?
-Peter