共用方式為


Creating Property Procedures

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

As mentioned previously, public module-level variables in a class module function as properties of an object. However, they're not very sophisticated. If you need to run code in order to set or return a property's value, or you want to make a property read-only, you can create a Property procedure. There are three types of Property procedures: PropertyGet, PropertyLet, and PropertySet procedures. The PropertyGet procedure returns the current value of a property, whereas the PropertyLet procedure sets the value. The PropertySet procedure assigns an object to an object property.

To create a read-write property, you need to include a pair of Property procedures in the class module. Both procedures must have the same name. If the property stores and returns a scalar value, such as a numeric, text, or date value, you use a PropertyLet procedure to set the value and a PropertyGet procedure to retrieve it. If the property stores and returns a reference to an object, you use the PropertySet procedure to store the reference and the PropertyGet procedure to return it.

You can also create read-only, write-only, and write-once properties. The following table outlines which property procedures you need for each type.

Type of property Procedures needed
Read-write, scalar Property Let, Property Get
Read-write, object Property Set, Property Get
Read-only, scalar Property Get
Read-only, object Property Get
Write-only, scalar Property Let
Write-once, scalar Property Let, including code to determine whether property has been set previously, Property Get
Write-once, object Property Set, including code to determine whether object property has been set previously, Property Get