Home > Language Reference > Statements

Property Statement

Declares the name, arguments and the body of a property.

Syntax

{ Public | Protected | Private } [ Static ] Property Get Name ([ByRef | ByVal] ArgList ) As type
[
Statements ]
[ Name=expression ]
[Exit Property ]
[Statement ]
[ Name=expression ]
End Property

or

{ Public | Protected | Private } [ Static ] Property { Let | Set } Name ( [ ByRef | ByVal ] ArgList )
[
Statements ]
[Exit Property ]
[Statement ]
End Property

ParameterDescription
NameProperty name
ArglistProperty argument list
typeData type returned by the property
expressionValue of the property returned by the procedure defined by the Get statement.

Remarks

The Property statement declares a property, which is much like a function or a procedure. Actually, most of the information given for the Sub and Function statements also apply to properties, notably about scope, parameter passing, and return values. The only difference between a regular pprocedure and a property resides in the way they are called. While the former is explicitly invoked using the optional Call keyword, the latter is implicitly invoked when you use its value in an expression or when you affect a value to it using an equal sign.

A property declared using Property Get is only available for reading, which means that it can only appear in an expression, as a right-value. It must return a value, but it may or may not accept parameters. The following example declares and uses a simple property that always returns 1:

Public Property Get Foo() As Integer
  Foo = 1
End Property

Public Sub Test()
  MsgBox Foo   ' Call the Foo property and display 1
  MsgBox Foo+1 ' Call the Foo property and display 2
  Foo = 2      ' Error #2204, property Foo is read-only
End Sub

A property declared using Property Let or Property Set is only available for writing, which means that it can only appear in the left hand side of an affectation. It cannot return a value, but it must accept at least one parameter that will receive the value being affected to the property. The following example declares and uses a simply property:

Public Property Let Foo(ByVal i As Integer)
  MsgBox i
End Property

Public Sub Test()
  Foo = 12       ' Call the Foo property which displays 12
  Foo = Rnd()*10 ' Call the Foo property which display a random number
  MsgBox Foo     ' Error #2205, property Foo is write-only
End Sub

As with the Let et Set statement, you should use the Property Let form when the affected value is a scalar or a string value, while you should use the Property Set form when the affected value is an object reference.

For a discussion about properties from a conceptual point of view, please refer to the Properties section in the Programmer's Guide.

System requirements

SystemMinimal versionRemarks
Palm OSPalm OS 3.0N/A