Skip to main content

Writing a property procedure

A property procedure is a series of Visual Basic statements that allow a programmer to create and manipulate custom properties.

  • Property procedures can be used to create read-only properties for forms, standard modules, and class modules.
  • Property procedures should be used instead of Public variables in code that must be executed when the property value is set.
  • Unlike Public variables, property procedures can have Help strings assigned to them in the Object Browser.

When you create a property procedure, it becomes a property of the module containing the procedure. Visual Basic provides the following three types of property procedures.

Procedure Description
Property Let A procedure that sets the value of a property.
Property Get A procedure that returns the value a property.
Property Set A procedure that sets a reference to an object.

The syntax for declaring a property procedure is as follows.

[ Public | Private ] [ Static ] Property { Get | Let | Set } propertyname [( arguments )] [ As type ] statements End Property

Property procedures are usually used in pairs: Property Let with Property Get, and Property Set with Property Get. Declaring a Property Get procedure alone is like declaring a read-only property. Using all three property procedure types together is only useful for Variant variables, because only a Variant can contain either an object or other data type information. Property Set is intended for use with objects; Property Let isn't.

The required arguments in property procedure declarations are shown in the following table.

Procedure Declaration syntax
Property Get Property Getpropname (1, …, n) As type
Property Let Property Letpropname (1, …,,,, n, n +1)
Property Set Property Setpropname (1, …, n, n +1)

The first argument through the next to last argument (1, …, n) must share the same names and data types in all property procedures with the same name.

A Property Get procedure declaration takes one less argument than the related Property Let and Property Set declarations. The data type of the Property Get procedure must be the same as the data type of the last argument (n +1) in the related Property Let and Property Set declarations. For example, if you declare the following Property Let procedure, the Property Get declaration must use arguments with the same name and data type as the arguments in the Property Let procedure.

Property Let Names(intX As Integer, intY As Integer, varZ As Variant) 
 ' Statement here. 
End Property 
Property Get Names(intX As Integer, intY As Integer) As Variant 
 ' Statement here. 
End Property

The data type of the final argument in a Property Set declaration must be either an object type or a Variant.

Leave a comment

Your email address will not be published. Required fields are marked *

Format your code: <pre><code class="language-vba">place your code here</code></pre>