Property Get Statement

Description

Declares the name, arguments, and code that form the body of a Property procedure, which gets the value of a property.

Syntax

[Public | Private][Static] Property Get name [(arglist)][As type]
[statements]
[name = expression]
[Exit Property]
[statements]
[name = expression]
End Property

The Property Get statement syntax has these parts:

Part

Description

Public

Indicates that the Property Get procedure is accessible to all other procedures in all modules. If used in a private module (one that contains an Option Private statement) the procedure is not available outside the project.

Private

Indicates that the Property Get procedure is accessible only to other procedures in the module where it is declared.

Static

Indicates that the Property Get procedure's local variables are preserved between calls. The Static attribute doesn't affect variables that are declared outside the Property Get procedure, even if they are used in the procedure.

name

Name of the Property Get procedure; follows standard variable naming conventions, except that the name can be the same as a Property Let or Property Set procedure in the same module.

arglist

List of variables representing arguments that are passed to the Property Get procedure when it is called. Multiple variables are separated by commas.

type

Data type of the value returned by the Property Get procedure; may be Boolean, Integer, Long, Currency, Single, Double, Date, String (except fixed length), Object, or Variant. Arrays of any type can't be returned, but a Variant containing an array can.

statements

Any group of statements to be executed within the body of the Property Get procedure.

expression

Value of the property returned by the procedure defined by the Property Get statement.


The arglist argument has the following syntax and parts:

[Optional][ByVal | ByRef] varname[( )][As type]

Part

Description

Optional

Indicates that an argument is not required. If used, all subsequent arguments in arglist must also be optional and declared using the Optional keyword. All Optional arguments must be Variant.

ByVal

Indicates that the argument is passed by value.

ByRef

Indicates that the argument is passed by reference.

varname

Name of the variable representing the argument; follows standard variable naming conventions.

type

Data type of the argument passed to the Property Get procedure; may be Boolean, Integer, Long, Currency, Single, Double, Date, String (variable length only), Object, Variant, a user-defined type, or an object type.


Remarks

If not explicitly specified using either Public or Private, Property procedures are Public by default. If Static is not used, the value of local variables is not preserved between calls.

All executable code must be in procedures. You can't define a Property Get procedure inside another Sub, Function, or Property procedure.

The Exit Property keywords cause an immediate exit from a Property Get procedure. Program execution continues with the statement following the statement that called the Property Get procedure. Any number of Exit Property statements can appear anywhere in a Property Get procedure.

Like a Sub and Property Let procedure, a Property Get procedure is a separate procedure that can take arguments, perform a series of statements, and change the values of its arguments. However, unlike a Sub or Property Let procedure, a Property Get procedure can be used on the right-hand side of an expression in the same way you use a Function or a property name when you want to return the value of a property.

See Also

Function Statement, Property Let Statement, Property Set Statement, Sub Statement.

Example

This example uses the Property Get Statement to define a property procedure that gets the value of a property that identifies, as a string, the current color of a pen in a drawing package:


Dim CurrentColor As Integer
Const BLACK = 0, RED = 1, GREEN = 2, BLUE = 3

' Returns the current color of the pen as a string
Property Get PenColor() As String
    Select Case CurrentColor
        Case RED
            PenColor = "Red"
        Case GREEN
            PenColor = "Green"
        Case BLUE
            PenColor = "Blue"
    End Select
End Property

' The following line gets the color of the pen 
' calling the Property Get procedure.
ColorName = PenColor()