Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
get the code
more resources

Deal With Multiple Object Versions
Leverage .NET's object-oriented features to combine dynamic binding's flexibility with static binding's performance benefits.
by Paul Reynolds

Posted October 21, 2003

Technology Toolbox: VB.NET

Your applications might need to interact sometimes with multiple versions of a third-party product. For example, suppose you want an app to work with Outlook 2000, Outlook 2002, and Outlook 2003. If you program in VB, your natural tendency might be to use dynamic (late) binding throughout your code that interacts with the Outlook Object Model (OOM). This approach provides the flexibility you need, but it comes at a price.

ADVERTISEMENT

Static binding is more straightforward and faster than dynamic binding (see Table 1 and Additional Resources). The Common Language Runtime (CLR) and .NET languages use static binding by default for this reason. Static binding's type checking and IntelliSense help you push error detection back from run time to compile time, which improves your program's size, speed, and reliability.

If you maximize your use of static binding and minimize your use of dynamic binding, you can gain the benefits of both. I'll show you some object-oriented programming (OOP) techniques that let you do this, using a simple example application that notifies the user that Outlook has started, stopped, or received new mail (see Listing 1 and download the sample code).

The code in Listing 1 performs three main steps: It gets the COutlook object from COutlookManager, adds an event handler to the form to get statechanged events from COutlook, and handles the event. The interesting work occurs in the first step. The application interacts with the COutlook class to obtain the event notification and then get further details. This class contains all the methods and functionality the application requires from Outlook. The app doesn't care which version of Outlook is running; you have IntelliSense, type checking, and explicit assembly references at this point.

However, you can't implement all of the required Outlook functionality directly in the COutlook class. Doing so would either bind the app statically to a particular version of the OOM, or you'd need to use dynamic binding—but you want the best of both worlds. OOP comes to the rescue.

Inheritance facilitates code reuse and creates an implicit compatibility between base classes and derived classes. This allows you to generate an object from a derived class and use it anywhere an object from the base class is required. This means your application's code can reference a generic base class and include other code that provides a derived class object (because they're type-compatible) that contains different functionality. This capability—known as polymorphism—is probably OOP's most useful facet.




Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home