|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Thats one way of adding a property to a class, but note that client applications have full access to this propertythey 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.
Adding A Get/Let Property To A Code ComponentThe Testing Department is on the phone again. Why isnt your new code component safe? Programmers are setting your DayOfTheMonth property and then having all kinds of trouble. Well, you say, they shouldnt set it to values greater than 31. Its your job to watch for that, they say. If you want to have control when client applications set or get the properties in your classs 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 modules 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 its up to you to customize them.
Lets see an example. In this case, well add a new property, SafeValue, to the class named ExampleClass that weve 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 modules code window and select the Add Procedure item in the Tools menu. Set this propertys 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.
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, well 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, were 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, well 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 books accompanying CD-ROM.)
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 ComponentYou 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 menus 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).
|
|
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. |