Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


You can also let the Package and Deployment Wizard (see Chapter 30) register the DLL file when it’s installed.

Creating And Registering An Out-Of-Process Code Component

How do you create and register the real code component for use in actual client applications? There are two ways to create the actual code component: one if you’re creating an in-process code component (an ActiveX DLL), and one if you’re creating an out-of-process code component (an ActiveX EXE). We took a look at in-process code components in the previous topic, and we’ll look at out-of-process code components here.

To create the out-of-process code component’s EXE file, select the Make ProjectName.exe item in the Visual Basic File menu. How do you register that out-of-process code component with Windows? You just run the EXE file—that’s all it takes. You code component is now ready to use.

Using The Class Initialize Event

The Initialize event occurs when a client application creates an instance of a class, and you can use this event to initialize the object that’s being created. No arguments are passed to this event’s handler. Here’s an example. When we created the class ExampleClass in the NewClass code component earlier in this chapter, we set up a property named NewValue and initialized it to 1 in the Initialize event handler like this:

Option Explicit

Public NewValue As Single

Private Sub Class_Initialize()
    NewValue = 1
End Sub

Using The Class Terminate Event

An object’s Terminate event occurs when the object goes out of scope, or when you set it to the Nothing keyword (see “Destroying A Code Component Object” later in this chapter). You can use this event to clean up after the object, such as releasing allocated memory or resources.

Here’s an example that you can add to the class ExampleClass in the NewClass code component developed earlier in this chapter. In this case, we’ll use the Terminate event to display a message box to the users indicating that the ExampleClass object they’ve created is being terminated:

Private Sub Class_Terminate()
    MsgBox "Terminating now!"
End Sub


WARNING!  If you use the Visual Basic End statement in a program, the program is stopped immediately and you won’t get a Terminate event for any of the program’s objects.

Global Objects: Using Code Components Without Creating An Object

Sometimes it’s difficult to create objects from code components in client applications, because the client application doesn’t support coding to let you do so. To handle such cases, Visual Basic supports GlobalMultiUse code components. After adding one of these code components to a client application, you don’t need to create an object before using its properties and methods.

Properties and methods of a GlobalMultiUse object (or global object) are added to the global name space of any project that uses the object. In the client application, then, you can add a reference to the component, and the names of the global object’s properties and methods will be recognized globally. You make a code component a GlobalMultiUse object with the Instancing property.

The Instancing property indicates how you want your class to interact with client applications, if at all; here are the possible values of that property:

  Private—Other applications aren’t allowed access to type library information about the class and cannot create instances of it. Private objects are only for use within your component.
  PublicNotCreatable—Other applications can use objects of this class only if your component creates the objects first. Other applications cannot use the CreateObject function or the New operator to create objects from the class.
  MultiUse—Other applications are allowed to create objects from the class. One instance of your component can provide any number of objects created in this fashion. An out-of-process component can supply multiple objects to multiple clients; an in-process component can supply multiple objects to the client and to any other components in its process.
  GlobalMultiUse—Similar to MultiUse, with one addition: properties and methods of the class can be invoked as if they were simply global functions. It’s not necessary to explicitly create an instance of the class first, because one will automatically be created.
  SingleUse—Other applications are allowed to create objects from the class, but every object of this class that a client creates starts a new instance of your component. Not allowed in ActiveX DLL projects.
  GlobalSingleUse—Similar to SingleUse, except that properties and methods of the class can be invoked as if they were simply global functions. Not allowed in ActiveX DLL projects.


WARNING!  The properties and methods of a global object only become part of the global name space when the component is used in other projects. Inside the project where you’ve created the GlobalMultiUse class, objects created from the class are not automatically added to the global space.

Here’s an example using a hypothetical nonglobal component named Calendar. When you add a reference to that class, you usually have to create an object from it and then can use the object’s properties and methods this way:
Dim objCalendar As New Calendar

Private Sub cmdCalculateResult_Click()
    Text1.Text = objCalendar.GetDate
End Sub

On the other hand, if Calendar is a GlobalMultiUse component, you just need to add a reference to it and then you can use its properties and methods without creating an object:
Private Sub cmdCalculateResult_Click()
    Text1.Text = GetDate
End Sub


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.