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


That’s one way of adding a property to a class, but note that client applications have full access to this property—they can read it and set it anytime and any way they wish. If you want to restrict access to the values a property can be set to (for example, making sure a Date property only holds legal dates), use the property Get and Let methods covered in the next topic.


TIP:  Setting properties for an out-of-process component can be slow. If your client application sets many properties and then calls a method, it’s probably better to pass those values to the method directly rather than setting them as properties.

Adding A Get/Let Property To A Code Component

The Testing Department is on the phone again. Why isn’t your new code component safe? Programmers are setting your DayOfTheMonth property and then having all kinds of trouble. Well, you say, they shouldn’t set it to values greater than 31. It’s your job to watch for that, they say.

If you want to have control when client applications set or get the properties in your class’s properties, you use Get and Let methods to let the client application get or set the property. To set up a property with Get/Let methods in a class module, open the class module’s code window and select the Add Procedure item in the Tools menu, opening the Add Procedure dialog box you see in Figure 27.4. Select the Property option button in the Type box and the Public option button in the Scope box, and click on OK to close the Add Procedure dialog box. This adds the Get/Let procedures for this property, and it’s up to you to customize them.


Figure 27.4  The Visual Basic Add Procedure dialog box.

Let’s see an example. In this case, we’ll add a new property, SafeValue, to the class named ExampleClass that we’ve developed in the previous few topics in this chapter. This new property will be a Single that can only be set to values greater than 0. To add this new property to ExampleClass, open that class module’s code window and select the Add Procedure item in the Tools menu. Set this property’s name to SafeValue, as you see in Figure 27.4, select the Property and Public option buttons, as also shown in that figure, and click on OK. When you click on OK, Visual Basic adds two new procedures to the class module for this property:

Public Property Get SafeValue() As Variant

End Property

Public Property Let SafeValue(ByVal vNewValue As Variant)

End Property

When client applications want to get the value in the SafeValue property, the SafeValue Get procedure is called; when they want to set the value in the SafeValue property, the SafeValue Let procedure is called.


TIP:  In addition to Get/Let procedures, you can even have Set procedures, which you use if your property is itself an object. This is a relatively advanced topic, worth looking into only if you want to set up properties that are themselves objects with data members and methods.

Visual Basic has given this new property the default type of Variant, which you can change to the appropriate type for your property. In this case, we’ll make SafeValue a Single value this way:

Public Property Get SafeValue() As Single

End Property

Public Property Let SafeValue(ByVal vNewValue As Single)

End Property

At this point, we’re ready to add code to support the SafeValue property. For example, we can store the actual value in this property in an internal variable named, say, sngInternalSafeValue:

Option Explicit

Public NewValue As Single
Private sngInternalSafeValue As Single
...

Now when client applications ask for the value in the SafeValue property, we pass back the value in sngInternalSafeValue:

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

On the other hand, if client applications try to set the value in SafeValue, we’ll check first to make sure that value is greater than 0 before storing it in sngInternalSafeValue:

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

In this way, we can control what values are stored in the properties in code components. The code for this example, ExampleClass.cls version 2, appears in Listing 27.2. (The final version, version 4, of this code is located in the ExampleClass folder on this book’s accompanying CD-ROM.)


TIP:  Setting properties for an out-of-process component can be slow. If your client application sets many properties and then calls a method, it’s probably better to pass those values to the method directly rather than setting them as properties.

Listing 27.2 ExampleClass.cls version 2

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

Adding A Method To A Code Component

You can add a method to a class in a code component as easily as you add a property. Methods can be either public subroutines or functions, and you can add them to a class module with the Tools menu’s Add Procedure item.

Besides declaring procedures 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 procedures as Friend procedures, they are available to the other objects in your code component, but not to client applications (that is, Friend works as a sort of local Public declaration).


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.