|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
The result appears in Figure 27.2, where we learn that 2 plus 2 equals 4. Congratulations, youve created your first code component client application. The code for this example is located in the codeclient folder on this books accompanying CD-ROM.
Creating An Object From A ClassWhen 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 (well use the CreateObject technique in this chapter):
Lets see some examples. Here, well assume weve already added a reference to the code component containing the classes well work with. If the code component youre 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 were 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, heres 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 fromas Excel.Sheet (that is, as CodeComponent.Class). Because the code components you create with CreateObject dont 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 its usually used to assign a reference to an existing object. Heres how you use GetObject: Set objectvariable = GetObject([ pathname] [, class]) Heres what the arguments of this function mean:
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, heres how we create an object of the class ExampleClass in the code component NewClass using GetObject (once again, we refer to the class were using as CodeComponent.Class):
Set objNewClass = GetObject("", "NewClass.ExampleClass")
Using A Code Components Properties And MethodsNow that youve 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 objects properties as you would any Visual Basic propertywith 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 ComponentThe Testing Department is on the phone again. The sorting routine youve written is great, but how about making it available to the rest of the companys programmers? Sure, you say, Ill 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:
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 youll 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. Well 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.
|
|
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. |