![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
This is all so similar to the way forms work that classes would only deserve a brief mention in this chapter except for one new twist: A program can make class objects available to other applications. One application can create and manipulate objects that are defined in another. It takes a little thought to understand the power of this seemingly trivial ability. Classes are more than just abstract data types like those defined using the Type statement. They can include subroutines and functions that perform complex actions. In fact, the subroutines and functions could perform all of the actions of a complete application. Suppose a class performed all the tasks needed by a full-featured word processor. Then any other application could create objects of that class and incorporate all of the word processors features. For example, it would be easy to add spell checking, dictionary, and thesaurus functions to an electronic mail application. This allows developers to reuse the code in the word processing application easily, without rewriting the word processing code. OLE defines how applications use this sort of class interaction. When an application defines a class for use by other programs, that application is called an OLE server. Other programs that use the server to create objects are called OLE clients. An application can use classes in OLE servers to reuse the code implemented by the servers. Some commercial applications expose objects that other applications can use. Some commercial applications can even use server classes defined by a Visual Basic application. The Visual Basic programming environment is one such application. It can use specially constructed server classes as add-ins to make common programming chores easier. Creating Add-InsThere are four steps to creating an application that can act as an add-in OLE server. The steps are listed below and described in the following sections:
Creating and installing an add-in is a complicated process. After the following four sections describe the process, a summary presents a detailed list of the steps required. Setting Project OptionsTo make a project behave as a proper add-in, several of its properties must be set. These properties are slightly different in Visual Basic 4 and Visual Basic 5. Project Properties in Visual Basic 4 In Visual Basic 4 these properties are set by selecting the Options command on the Tools menu. The Project tab of the Options dialog is shown in Figure 4.3. Three options must be set on the project Options dialog to create an add-in: Startup Form, Project Name, and StartMode. OLE servers, including add-ins, do not need to display startup forms. Instead, Visual Basic 4 begins execution with a subroutine named Main. To indicate that the project will start by running this subroutine, the Sub Main option should be selected from the Startup Form list. The Project Name option determines the name by which the server will be known to the system registry. Other applications will use this name to access the server. For example, if a projects name is set to MyProject in the options dialog, and the project defines a server class named MyClass, then another application could refer to the server class as MyProject.MyClass. Because other applications will need to use the project name, it should be relatively short and easy to remember.
Normally an OLE server runs only when it is serving a client application. If an OLE server program is started from within the Visual Basic programming environment, no client will be ready for the server so the program will immediately end. For testing purposes, however, it is convenient to keep a server running even if it has no clients. Then a client application can invoke the server and connect to the running application. Checking the OLE Server option button in the project Options dialog tells the Visual Basic development environment to keep the program running even if it has no clients. This option affects the application only when it is running in the Visual Basic development environment. This option does not change the programs behavior when it is running as a normal OLE server. Project Properties in Visual Basic 5 In Visual Basic 5, the Properties command in the Project menu presents the project Options dialog. The General tab of the Project Properties dialog is shown in Figure 4.4. In Visual Basic 5, only two options must be set on the Project Properties dialog: Project Type and Project Name. Setting the project type to ActiveX DLL or ActiveX EXE indicates that the application is an ActiveX or OLE server. This tells Visual Basic to keep the application running in the development environment even if it does not have a client, much as selecting the OLE Server option does in Visual Basic 4. It also makes the Startup Object irrelevant. In Visual Basic 5, this object can be set to either Sub Main or (None). In either case, when Visual Basic 5 runs an OLE server in the development environment, it does not execute Sub Main. This has consequences that are described in the later section, Creating Sub Main.
The Project Name should be set as it is for Visual Basic 4. Creating the Add-In ClassIf a class is to be used as an add-in, it must have certain standard property values, and it must provide certain standard subroutines. Some of these are slightly different in Visual Basic 4 and Visual Basic 5. The following two sections, Class Properties in Visual Basic 4 and Class Properties in Visual Basic 5, describe the properties that should be set for an add-in. The next three sections describe the standard Connect-AddIn, DisconnectAddIn, and AfterClick subroutines that should be provided by the add-in class. Class Properties in Visual Basic 4 In Visual Basic 4, the Public property of the add-in class should be set to true. This can be done using the class modules Properties window. Setting Public to true allows client applications to create instances of the class. Because the Visual Basic development environment is not part of the application, it needs permission to create objects of this class. The class modules Instancing variable should be set to Creatable SingleUse (1) or Creatable MultiUse (2). This indicates how the server should provide objects from the class. If Instancing is set to Creatable SingleUse, the system will create a separate server for each client requesting an instance of the class. If Instancing is set to Creatable MultiUse, the system will allow one server to serve more than one client at a time. The clients will be assigned different class objects, but they will run in the same execution space. If the server application uses global variables, all of the clients will have access to the same global variables through their server objects. Because the system will not need to start several servers to satisfy multiple client requests, satisfying clients may be faster if Instancing is Creatable MultiUse. On the other hand, the server must be careful that multiple clients do not interfere with each other while accessing global variables.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|