|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
In fact, theres an easier way to specify a default valueyou just append = DefaultValue to the argument in the argument list as in this case, where we make vbBlack the default for the lngColor argument:
Public Sub DrawRect(intWidth As Integer, intHeight As Integer, Optional_
lngColor As Single = vbBlack)
Dim rectangle As New RectangleType
...
End Sub
Thats itnow weve made use of optional arguments in our class methods. Testing A Code Component With A Second Instance Of Visual BasicThe Testing Department is on the phone again. Didnt you test your code component before releasing it? You say, test it? You can test a code component while developing it by registering it with Windows temporarily and letting Visual Basic handle all the details. All you have to do is to open the Project|Properties menu item, click the Debugging tab, make sure the option button labeled Wait For components To Be Created is selected, click on OK to close the Properties dialog box, and run the program. This registers your code component with Windows temporarily and makes it available to client applications until you stop the code component execution. How do you make use of the code component youre testing in a client application? One easy way is to start a second copy of Visual Basic and run the client program there after adding a reference to the code component youre testing. Lets see an example. Here, well test the NewClass code component that weve developed in the previous few topics in a client application. That project supports a class, ExampleClass, which has properties and methods, and well put them to use in the client application. Load that code component into Visual Basic, as well as the client application, after setting the debugging options as indicated in the previous paragraph. Now start a second instance of Visual Basic and begin the new client application (a standard EXE project), which well call ExampleClassApp, by adding a reference to our code component, NewClass, using the Project|References menu item. Next, well create an object of the ExampleClass class named, say, objExample (make this object a form-wide variable by declaring it in the (General) area): Dim objExample As ExampleClass When the form loads, we create that object using CreateObject this way; note the way we specify the class name to create the object fromas NewClass.ExampleClass (that is, as CodeComponent.Class)when we use CreateObject:
Private Sub Form_Load()
Set objExample = CreateObject("NewClass.ExampleClass")
...
End Sub
You can also take care of any needed initialization in the Form Load event handler, as in this case, where we set the new objects SafeValue property to 3:
Private Sub Form_Load()
Set objExample = CreateObject("NewClass.ExampleClass")
objExample.SafeValue = 3
End Sub
There are two properties and two methods in ExampleClass, so we add four command buttons to the client application, as well as a text box, Text1, to show the results of testing these properties and methods. When the user clicks Command1, we can read and display the value in the objExamples NewValue property:
Private Sub Command1_Click()
Text1.Text = "The NewValue property = " objExample.NewValue
End Sub
When the user clicks Command2, we set the objExamples SafeValue (which is supported with Get and Let methods) to 5 and then display the new value:
Private Sub Command2_Click()
objExample.SafeValue = 5
Text1.Text = "The SafeValue property was set to 5 and returns: " &_
objExample.SafeValue
End Sub
Well test the ReturnSafeValue method when the user clicks Command3. That method takes no parameters and returns the value in the SafeValue property:
Private Sub Command3_Click()
Text1.Text = "The ReturnSafeValue method returns: " & _
objExample.ReturnSafeValue
End Sub
Finally, when the user clicks Command4, we test the Addem method, which adds two numbers, by adding 2 and 2:
Private Sub Command4_Click()
Text1.Text = "Adding 2 + 2 with Addem yields: " & _
objExample.Addem(2, 2)
End Sub
Thats all it takes. Now run the client application weve just developed, as shown in Figure 27.5. You can see the client application running in that figure and testing the ExampleClasss properties and methods; behind it is the copy of Visual Basic in which weve developed the client application, and behind that is the copy of Visual Basic in which the code component, NewClass (which contains the ExampleClass class), is running. Our test is a success. The code for the client application is located in the exampleclassapp folder on this books accompanying CD-ROM.
Creating And Registering An In-Process Code ComponentIn the previous topic, we showed how to test code components using two copies of Visual Basicbut 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 youre creating an in-process code component (an ActiveX DLL), and one if youre creating an out-of-process code component (an ActiveX EXE). Well take a look at in-process code components here and out-of-process code components in the next topic. To create the in-process servers DLL file, select the Make ProjectName.dll item in the Visual Basic File menu. To register that DLL file with Windows, you use a Windows utility like regsvr32.exe like this: c:\>c:\windows\system\regsvr32 ProjectName.dll
|
|
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. |