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 fact, there’s an easier way to specify a default value—you 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

That’s it—now we’ve made use of optional arguments in our class methods.

Testing A Code Component With A Second Instance Of Visual Basic

The Testing Department is on the phone again. Didn’t 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 you’re 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 you’re testing. Let’s see an example. Here, we’ll test the NewClass code component that we’ve developed in the previous few topics in a client application. That project supports a class, ExampleClass, which has properties and methods, and we’ll 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 we’ll call ExampleClassApp, by adding a reference to our code component, NewClass, using the Project|References menu item.

Next, we’ll 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 from—as 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 object’s 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 objExample’s NewValue property:

Private Sub Command1_Click()
    Text1.Text = "The NewValue property = " objExample.NewValue
End Sub

When the user clicks Command2, we set the objExample’s 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

We’ll 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

That’s all it takes. Now run the client application we’ve just developed, as shown in Figure 27.5. You can see the client application running in that figure and testing the ExampleClass’s properties and methods; behind it is the copy of Visual Basic in which we’ve 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 book’s accompanying CD-ROM.


Figure 27.5  Testing a code component by running a second copy of Visual Basic.

Creating And Registering An In-Process Code Component

In the previous topic, we showed how to test code components using two copies of Visual Basic—but 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 you’re creating an in-process code component (an ActiveX DLL), and one if you’re creating an out-of-process code component (an ActiveX EXE). We’ll take a look at in-process code components here and out-of-process code components in the next topic.

To create the in-process server’s 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


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.