Create Custom Properties for Existing Objects


Now that you have worked with existing objects and created your own objects, you will see a few ways to extend existing objects, giving them additional properties, methods, or events. Start by looking at how to create custom properties for existing objects.

Using Existing Tag Property

One way to create a custom property for an existing object is to use the Tag property. The Tag property is only used when you write code to set the value and then use that value later for some specified purpose. For example, you might use the Tag property to hold a value and then retrieve that value at a later time.

Using Public Variables

As described earlier, when you create properties for your custom objects, you can also create properties for existing objects, using public variables. To do so, just add the public variable declaration to the class module of the existing object, such as an existing class for a form. Again, this is not the recommended way to create properties. As discussed previously, I do not recommend this approach for three primary reasons. First, you cannot manipulate the value because the property is set; second, you cannot create read-only and write-only properties; third, you cannot easily validate what is assigned to public variables. A better way to create properties, discussed next, does not have these drawbacks.

Using Property Let, Property Get, and Property Set Routines

You can use the Property Let, Property Get, and Property Set procedures to create properties for existing objects. Again, you can do this just as you did for your own objects, only you add the PropertyLet and PropertyGet procedure to the existing object. Now add a new property to the frmCarDetails form to see how this works.

Try It Out-Adding New Property for frmCarDetails Form

image from book
  1. From the Visual Basic Editor, select the frmCarDetails class module. Add the following declaration to the General Declarations section of the class:

      Dim strTestPropertyVal As String 

  2. Also from the frmCarDetails class module, add the following property procedures:

      Public Property Get TestProperty() As String     TestProperty = strTestPropertyVal End Property Public Property Let TestProperty(ByVal Value As String)      strTestPropertyVal = Value End Property 

  1. In the frmCarDetails class module, add a new procedure called ShowTestProperty and type the Me keyword followed by a period, as shown in Figure 4-15. Note that the TestProperty that you just added in Step 2 now appears, and you can select it from the list and assign it a value if desired. You can delete the ShowTestProperty procedure, as it was just used to show you that the new property you just added is now available.

    image from book
    Figure 4-15

How It Works

First, you added the property procedures to the existing frmCarDetails form. Then, when you referred to frmCarDetails in code, you saw that the custom property you just added was in the drop-down list along with the other original properties.

Properties can be added to nearly any existing object. Custom properties that you add are not illustrated in the Properties panel, but are visible in the Code window as shown in Figure 4-15.

image from book




Beginning Access 2007 VBA
Beginning Access 2007 VBA
ISBN: 0470046848
EAN: 2147483647
Year: 2004
Pages: 143

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net