If you skipped Chapter 3 in order to go directly to the COM chapter, do not pass Go, do not collect $200. Under Visual Basic, components must be object-
oriented. You can’t implement them without implementing classes. You can look at this as a hoop to jump through and simply turn your functions into methods of a global class (as described in Chapter 5). Or you can take it as an opportunity to rethink your application. As a sample, let’s implement the sieve of Eratosthenes ActiveX component.
The sieve of Eratosthenes algorithm has traditionally been used as a benchmark for measuring the performance of different machines, operating systems, programming languages, and compilers and interpreters. Here I’ll expand it to measure the difference between different kinds of ActiveX components. In the process, I’ll examine servers and clients in “slow motion.” You don’t really need to know every detail in order to create or use components, but since you’re hardcore, you might as well know the inside story.
I’ll show you both client and server written in Visual Basic because COM communication is easier to understand if you control both parts. But keep in mind that COM doesn’t know or care what language is used. Often, when you purchase a component, you won’t know what language it was written in. Similarly, if you create a component in Visual Basic, your clients need not know what language you used.
In real life, you’ll always need to create a test client to make sure that your component works. Visual Basic is the ideal language in which to write your test client, even if you choose to write the component in C++ or some other language. In fact, Visual Basic is an informal standard of what component containers should do. If your component behaves badly in Visual Basic, it will probably behave badly in other containers that emulate Visual Basic. On the other hand, if you do write your component in
Visual Basic, that doesn’t give you an excuse to assume your
clients will do the same. Curses on all those arrogant developers who write public components specifically for their favorite language. The market will punish you.