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


In this case, we’ll name the new project NewClass, so just enter that name in the Project Name field now. In addition, you can’t use a form as a startup object in a code component, so select (None) in the Startup Object box, as shown in Figure 27.3. We’ll make this example an ActiveX EXE project, so select that type in the Project Type box, as shown in the figure. Now click on OK to close the Project Properties window.


TIP:  You can use Sub Main as the startup object in a code component, adding code to that procedure if you wish. For example, you may want to display a form when your code component first loads, in which case you could do that from Sub Main (although doing so is not recommended for ActiveX DLLs).

Even though we don’t have a startup object now, we still have a form, Form1, in our project (which you won’t have if you create an ActiveX DLL or EXE project directly from the New Project window), and you can remove that form in your code component projects as you like. We’ll leave it in the NewClass project to show how to use forms in code components later in this chapter.

At this point, then, we’ve created a new code component. However, this new code component doesn’t have any classes in it, so we’ll add one now. To add a class to our code component, we add a class module. To do that, select the Project|Add Class Module menu item, select the Class Module icon in the Add Class Module dialog box that appears, and click Open.

This creates a new class module and brings us up to exactly where we’d be if we had used the ActiveX EXE icon in the New Project window instead of creating this project ourselves. When you save the new class module in a file, the file will have the extension .cls, for class.

Now set the class module’s Name property to the name you want to use for your new class—we’ll use ExampleClass here. That’s it—we’ve created a new class. However, there’s not much going on in this class, so we’ll add a property, NewValue, to the class ExampleClass here. To do that, just add this code to the class module:

Option Explicit

Public NewValue As Single
...

Declaring a variable as Public in a class module makes it available as a property to client applications, as we’ll see when we discuss properties later in this chapter. To set the value of our new property to, say, 1, we add code to the class’s Initialize event, which occurs when the code component is first loaded:

Option Explicit

Public NewValue As Single

Private Sub Class_Initialize()
    NewValue = 1
End Sub

And that’s it—we’ve created a new code component and a new class that supports a property. To make this component available to client applications, just create its EXE file using the Make NewClass.exe item in the File menu, and run that EXE file to register the code component with Windows. We’ll see how to put this new code component to work in the following topics in this chapter.

The code for this example, ExampleClass.cls version 1, appears in Listing 27.1. (The final version, version 4, is located in the ExampleClass folder on this book’s accompanying CD-ROM.)

Listing 27.1 ExampleClass.cls version 1

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "ExampleClass"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit

Public NewValue As Single

Private Sub Class_Initialize()
    NewValue = 1
End Sub

Setting A Code Component’s Project Type: In-Process Or Out-Of-Process

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.

There are advantages to both types. For example, an in-process component can run faster, but an out-of-process component can be self-registering (just run the EXE file to register the component with Windows).

Adding A Property To A Code Component

The Testing Department is on the phone again. Your new code component’s class is great, by why doesn’t it do anything? How about adding a property to it? Hmm, you think, how does that work?

You can add properties to a class in a code component in two ways: by declaring them as Public variables or by using Let and Get functions. We’ll take a look at the first technique here and the second technique in the next topic in this chapter.

The first way of creating properties in a class module is very easy: you just declare the property you want as a Public variable in the class module’s (General) declarations area. Making a variable public in a class module makes it a property of that module. Besides declaring variables Public in a class module, you can also declare them as Private, which means they are not available outside the module, and as Friend. When you declare variables as Friend variables, they are available to the other objects in your code component, but not to client applications (in other words, Friend works as a sort of local Public declaration).

Let’s see an example. In “Creating An Object From A Class” earlier in this chapter, we created a class named ExampleClass and added a property to that class—NewValue—by simply declaring that property as a public variable:

Option Explicit

Public NewValue As Single
...

In addition, we initialized the value of that property when the class was first loaded with the Initialize event:

Option Explicit

Public NewValue As Single

Private Sub Class_Initialize()
    NewValue = 1
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.