If you develop applications that use several versions of a component,
you might have read "Polymorphism, Interfaces, Type Libraries, and
GUIDs" in VB's Help file. Microsoft gives an example of how to test
whether an object supports a particular interface:Dim fnr As FinanceRules
Dim ifin As IFinance
Dim ifin2 As IFinance2
On Error Resume Next
Set fnr = New FinanceRules
' (Error handling code omitted.)
' Attempt to access the preferred interface.
Set ifin2 = fnr
If Err.Number <> 0 ThenIf Err.Number <> 0 Then
' Access the more limited interface.
Set ifin = fnr
' (Code to provide limited functionality,
' using the object variable ifin.)
Else
' (Code to provide full functionality,
' using the object variable ifin2.)
End If
As you can see, this example uses inline error handling to determine
whether a type-mismatch error occurs when setting ifin2 = fnr. If your
routine already has an active error handler, your code can become
cluttered with this solution--especially if you test for several
different interfaces in a single routine.
To make your code more readable, use:On Error Goto YourErrorHandler
Set fnr = New FinanceRules
' (Errors produced by previous line are handled
' in YourErrorHandler).
' Check to see if fnr supports the IFinance2 interface
If Not TypeOf fnr Is IFinance2 Then
' Access the more limited interface.
Set ifin = fnr
' (Code to provide limited functionality,
' using the object variable ifin.)
Else
set ifin2 = fnr
' (Code to provide full functionality,
' using the object variable ifin2.)
End If
Note that you can't use If TypeName(fnr) <> "IFinance2" Then
because TypeName() always returns the actual class name of the
instantiated object, not the interfaces it implements.
Testing Visual Basic class interfaces
By ElementK Journals, published on 14 Jul 2001
| Filed in
You might also like...
TDD forum discussion
-
CorelDRAW VBA: cdrTraceLineDrawing FAILS, producing single linear path instead of Centerline trace?
by dancemanj (0 replies)
-
client/server application using activex
by beautifulheart (0 replies)
-
System Error &H8007007E. The specifed module could not be found.
by swiftsafe (5 replies)
-
Invitation to take part in an academic research study
by researchlab (0 replies)
-
Send SMS with SMPP
by mmahmoud (0 replies)
TDD podcasts
-
Hanselminutes: Understanding BDD and NSpec with Matt Florence and Amir Rajan
Published 9 years ago, running time 0h34m
Scott sits down with NSpec authors Matt Florence and Amir Rajan to talk about Behavior Driven Development (BDD). Where does one start with BDD? Is BDD just TDD with a fancier name or can it really chance how you design software? The NSpec guys set Scott on the right path.
TDD jobs
-
RoR Developer @ Wimdu
Wimdu GmbH in Berlin, Germany
Comments