Property Statement

This statement begins a user-defined Property procedure. It specifies the return data type and any parameters that are passed when it is called.

[Public | Private | Shared | ReadOnly | WriteOnly ]Propertyproperty_name ([ parameter_list ]) Astype)

Prerequisites

Parameters

property_name

The name of the Property to be defined.

parameter_list

A list of parameters that are passed to the Property when it is called. Properties often have an empty parameter list. Each parameter appears as a locally defined variable and is associated with a value when the procedure is called. The caller must provide arguments that match the number and type of the parameters specified in this statement. The list may be empty if the Property has no parameters. Multiple parameter list elements are separated by ",". Each element has the form:

[ ByVal | ByRef ] parameter_nameAstype

parameter_name

The name of the variable associated with this parameter. This name is known only within the procedure being defined.

type

The type of this parameter. The type may be a primitive type, the name of a built-in class, or the name of a user-defined class. The primitive type keywords are:

Boolean, Byte, Double, Integer, Short, Single

If a class name is specified, the variable becomes an object variable.

Either ByVal or ByRef can be specified, but not both. If neither is specified, the default is ByVal. A ByVal parameter receives a copy of the argument value from the caller. The local procedure can change the value without affecting the caller’s value. A ByRef parameter references the caller’s value directly. Any changes to a ByRef parameter in the called routine are reflected in the calling routine. Since object variables always deal with pointers to object values, the called routine can always change an object value, even when passed using a ByVal parameter.

type

The type of the value returned by this Property. The type may be either a primitive type, the name of a built-in class, or the name of a user-defined class. The primitive type keywords are:

Boolean, Byte, Double, Integer, Short, Single

If a class name is specified, the returned type is an object.

Remarks

Property procedures may set a value or get (return) a value. Property procedures that set a value must include a set procedure block that begins with a Set statement and ends with an End Set statement. The property_name and parameter_list may be used on the left-hand side of an assignment statement. A Property procedure that gets a value must include a get procedure block that begins with a Get statement and ends with an End Get statement. A Get Property may be used just like a Function within an expression or on the right-hand side of an assignment statement, where a value of the proper type is allowed.

A Property definition must always end with an End Property statement.

If the Property contains only a get procedure, the ReadOnly keyword must be specified. If the Property contains only a set procedure, the WriteOnly keyword must be specified. A property procedure exits when it encounters the End Property statement, an Exit Property statement or a Return statement. If Public is specified, this procedure can be called from other modules or classes. Otherwise it can only be called from within the class where it is defined. If the Shared keyword appears, the property is associated with the entire class rather than with a particular object of that class type.

Examples

Class cc
Private size_value As Integer

Public Property size As Integer 'Set size, clip value at 10
Set (value As Integer)
If value > 10 Then
value = 10
End If
size_value = value
End Set
Get
Return size_value
End Get
End Property

End Class
:
Dim obj As New cc
obj.size = 20 ' Sets size_value
Console.WriteLine(obj.size) ' Displays 10

See Also

Statements |GetStatement| SetStatement