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


The result appears in Figure 27.2, where we learn that 2 plus 2 equals 4. Congratulations, you’ve created your first code component client application. The code for this example is located in the codeclient folder on this book’s accompanying CD-ROM.


Figure 27.2  Using Microsoft Excel as a code component.

Creating An Object From A Class

When you want to create an object from a code component, you first add a reference to that code component using the Visual Basic Project|References menu item (note that the code component must be registered with Windows, which most applications will do automatically when installed). Next, you create that object in code, and there are three ways to create objects from code components in Visual Basic (we’ll use the CreateObject technique in this chapter):

  Declaring the variable using the New keyword (in statements like Dim, Public, or Private), which means Visual Basic automatically assigns a new object reference the first time you use the variable (for example, when you refer to one of its methods or properties). This technique only works with code components that supply type libraries (as the code components created with Visual Basic do). These type libraries specify what’s in the code component.
  Assigning a reference to a new object in a Set statement by using the New keyword or CreateObject function.
  Assigning a reference to a new or existing object in a Set statement by using the GetObject function.

Let’s see some examples. Here, we’ll assume we’ve already added a reference to the code component containing the classes we’ll work with. If the code component you’re using supplies a type library, as the ones built with Visual basic do, you can use the New keyword when you declare the object to create it. As an example, here we’re creating an object named objSorter of the Sorter class:

Dim objSorter As New Sorter
objSorter.Sort

The object is actually only created when you refer to it for the first time.

Whether or not a code component supplies a type library, you can use the CreateObject function in a Set statement to create a new object and assign an object reference to an object variable. To use CreateObject, you pass it the name of the class you want to create an object of. For example, here’s how we created a new object named objExcel from the Microsoft Excel code component library in the previous topic, using CreateObject:

Dim objExcel As Object
Set objExcel = CreateObject("Excel.Sheet")

Note the way we specify the class name to create the object from—as Excel.Sheet (that is, as CodeComponent.Class). Because the code components you create with CreateObject don’t need a type library, you must refer to the class you want to use as CodeComponent.Class instead of just by class, as you can with New.

You can also use the GetObject function to assign a reference to a new class, although it’s usually used to assign a reference to an existing object. Here’s how you use GetObject:

Set objectvariable = GetObject([ pathname] [, class])

Here’s what the arguments of this function mean:

  pathname—The path to an existing file or an empty string. This argument can be omitted.
  class—The name of the class you want to create an object of. If pathname is omitted, then class is required.

Passing an empty string for the first argument makes GetObject work like CreateObject, creating a new object of the class whose name is in class. For example, here’s how we create an object of the class ExampleClass in the code component NewClass using GetObject (once again, we refer to the class we’re using as CodeComponent.Class):

Set objNewClass = GetObject("", "NewClass.ExampleClass")

Using A Code Component’s Properties And Methods

Now that you’ve created an object from a code component, how do you use its properties and methods? You access them in the same way you would the properties and methods of intrinsic Visual Basic objects. For example, we created a client application at the beginning of this chapter that created an object, objExcel, from the Microsoft Excel library, and then we used that object’s properties as you would any Visual Basic property—with the dot operator:

Private Sub Command1_Click()
    Dim objExcel As Object
    Set objExcel = CreateObject("Excel.Sheet")

    objExcel.Cells(1, 1).Value = "2"
    objExcel.Cells(2, 1).Value = "2"

    objExcel.Cells(3, 1).Formula = "=R1C1 + R2C1"
    MsgBox "Microsoft Excel says: 2 + 2 = " & objExcel.Cells(3, 1)

    objExcel.Application.Quit
End Sub

Creating A Code Component

The Testing Department is on the phone again. The sorting routine you’ve written is great, but how about making it available to the rest of the company’s programmers? Sure, you say, I’ll print it out now. Not that way, they say, create a code component.

How do you create a code component? There are two types of code components: in-process components and out-of-process components:

  When you create a code component as an ActiveX DLL, it will run as an in-process component, in the same process as the client application.
  When you create a code component as an ActiveX EXE, it will run as an out-of-process component, in a different process as the client application.

And, as it turns out, there are two ways to create each type of code component in Visual Basic. From the New Project window, you can create an ActiveX DLL or ActiveX EXE by selecting the appropriate icon and clicking on OK, which creates the files you’ll need. Or you can set the project type using the Project|Properties menu item in a standard project to convert it to an ActiveX DLL or ActiveX EXE project.

We’ll take a look at the second way here, because that will give us some insight into how code components are constructed (to use the shorter method, all you have to do is to click the ActiveX DLL or ActiveX EXE icon in the New Project window). Create a new standard Visual Basic project now, and select the Project|Properties menu item, opening the Project Properties dialog box you see in Figure 27.3.


Figure 27.3  The Project Properties dialog box.


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.