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


Let’s see an example. Here, we’ll add a method to the class named ExampleClass that we’ve been developing in the previous few topics. This new method, ReturnSaveValue, will return the value currently in the class’s SafeValue property. To add this method, open the ExampleClass’s code window and select the Tools menu’s Add Procedure dialog box. Using that dialog box, add a new public function named ReturnSafeValue. When you close the dialog box by clicking on OK, Visual Basic adds this code to the class module:

Public Function ReturnSafeValue()

End Function

As it stands, this function returns a Variant, so we change that to make it return a value of the same type as the SafeValue property, which is Single:

Public Function ReturnSafeValue() As Single

End Function

Now we’re ready to write the code for this method, which just returns the value in the SafeValue property, and that value is stored in the sngInternalSafeValue variable:

Public Function ReturnSafeValue() As Single
    ReturnSafeValue = sngInternalSafeValue
End Function

And that’s all it takes—now we’ve added a new method to the class named ExampleClass.

Passing Arguments To A Code Component Method

You can pass arguments to class methods just as you can to other procedures—just specify an argument list in the method’s declaration.

Because of marshaling, certain method arguments should be declared ByVal for out-of-process components and ByRef for in-process components. When you’re declaring methods for objects provided by an out-of-process component, use ByVal to declare arguments that will contain object references. In addition, if you expect client applications to pass large strings or Variant arrays to a method, and the method does not modify the data, declare the parameter ByVal for an out-of-process component, but ByRef for an in-process component.

Let’s see an example. In this case, we’ll add a method named Addem to the class named ExampleClass that we’ve developed over the previous few topics. This function will take two integer arguments, add them, and return their sum.

To add this new function, open ExampleClass’s code window, and select Add Procedure in the Tools menu, adding a public function named Addem:

Public Function Addem()

End Function

Here, we modify the declaration of the function to indicate that it takes two integers and returns another integer:

Public Function Addem(Operand1 As Integer, Operand2 As Integer) As _
    Integer

End Function

Finally, we add the two arguments passed to us in Addem and return the sum:

Public Function Addem(Operand1 As Integer, Operand2 As Integer) As _
    Integer
    Addem = Operand1 + Operand2
End Function

And that’s all it takes—now we’ve added a new method to ExampleClass, which takes two arguments, adds them, and returns the result. Our arguments example is a success.

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

Listing 27.3 ExampleClass.cls version 3

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 sngInternalSafeValue As Single

Private Sub Class_Initialize()
    NewValue = 1
End Sub

Public Property Get SafeValue() As Single
    SafeValue = sngInternalSafeValue
End Property

Public Property Let SafeValue(ByVal vNewValue As Single)
    If vNewValue > 0 Then
        sngInternalSafeValue = vNewValue
    End If
End Property

Public Function ReturnSafeValue() As Single
    ReturnSafeValue = sngInternalSafeValue
End Function

Public Function Addem(Operand1 As Integer, Operand2 As Integer) As Integer
    Addem = Operand1 + Operand2
End Function

Passing Optional Arguments To A Code Component Method

You can set up code component methods to accept optional arguments, just as the Windows calls in Chapter 23 did. To make an argument in an argument list optional, you use the Visual Basic Optional keyword when declaring a subroutine or function (note that if you declare an argument in an argument list Optional, all following arguments must be Optional as well).

Let’s see an example. Here, we create a hypothetical code component method, a subroutine named DrawRect, to draw a rectangle. We pass the width and height to that subroutine, as well as an optional argument to set the rectangle’s color. If the programmer using DrawRect does not specify a color for the rectangle, we’ll make it black.

To start, we declare the subroutine, making the color argument optional:

Public Sub DrawRect(intWidth As Integer, intHeight as Integer, Optional_
    lngColor As Long)
...

Now we declare a rectangle from some hypothetical type named RectangleType and set its width and height from the arguments passed to us:

Public Sub DrawRect(intWidth As Integer, intHeight as Integer, Optional_
    lngColor As Long)
    Dim rectangle as New RectangleType

    rectangle.width = intWidth
    rectangle.height = intHeight
...

We can also make use of the optional color argument—if it was passed to us. You can test if an argument was passed with the IsMissing function, allowing us to set the rectangle’s color this way if we were passed a color:

Public Sub DrawRect(intWidth As Integer, intHeight as Integer, Optional_
    lngColor As Long)
    Dim rectangle as New RectangleType

    rectangle.width = intWidth
    rectangle.height = intHeight

    If Not IsMissing(lngColor) Then
        rectangle.color = lngColor
...

On the other hand, if we were not passed a color value for the rectangle, we use black like this for the default color:

Public Sub DrawRect(intWidth As Integer, intHeight as Integer, Optional_
    lngColor As Long)
    Dim rectangle as New RectangleType

    rectangle.width = intWidth
    rectangle.height = intHeight

    If Not IsMissing(lngColor) Then
        rectangle.color = lngColor
    Else
        rectangle.color = vbBlack
    End If

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.