|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
In this case, well name the new project NewClass, so just enter that name in the Project Name field now. In addition, you cant 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. Well 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.
Even though we dont have a startup object now, we still have a form, Form1, in our project (which you wont 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. Well leave it in the NewClass project to show how to use forms in code components later in this chapter. At this point, then, weve created a new code component. However, this new code component doesnt have any classes in it, so well 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 wed 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 modules Name property to the name you want to use for your new classwell use ExampleClass here. Thats itweve created a new class. However, theres not much going on in this class, so well 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 well 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 classs 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 thats itweve 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. Well 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 books 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 Components Project Type: In-Process Or Out-Of-ProcessThere are two types of code components: in-process components and out-of-process components:
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 ComponentThe Testing Department is on the phone again. Your new code components class is great, by why doesnt 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. Well 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 modules (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). Lets 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 classNewValueby 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
|
|
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. |