Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Property Procedures

A Property procedure is a series of Visual Basic statements that manipulate a custom property on a module, class, or structure. Property procedures are also known as property accessors.

A property differs from a public variable or a field in the following respects:

  • You implement a property with executable code (its Property procedures), rather than with a single declaration statement.
  • Property procedures are executed when the property value is set or retrieved. This allows a class to perform custom actions when client code accesses the property.
  • A property does not have any storage location associated with its declaration. Although its Property procedures often define local variables and constants, these are usually not available to the code accessing the property. Therefore, while you can include a variable or a field as a member of a structure or array, you cannot do this with a property.
  • You can define a property as read-only, write-only, or read/write. The default is read/write.

Visual Basic provides the following Property procedures:

  • Get procedures return the value of a property; they are called when the property is accessed in an expression.
  • Set procedures set a property to a value, including an object reference; they are called when a value is assigned to the property.

You usually define Property procedures in pairs, using the Get and Set keywords, but you can define either procedure alone if the property is read-only (Get) or write-only (Set).

Property Declaration

A property itself is defined by a block of code enclosed within the Property and End Property statements. Inside this block, each Property procedure appears as an internal block enclosed within a declaration statement (Get or Set) and an End statement. The syntax for declaring a property and its procedures is as follows:

[Default] [accessibility] Property propertyname[(argumentlist)] As datatype
Get
   ' ...
   Return expression   ' expression is returned as property's value.
End Get
Set [(ByVal newvalue As datatype)]
   ' ...
   lvalue = newvalue   ' newvalue is the new value to be assigned.
End Set
End Property

The accessibility can be Public, Protected, Friend, Protected Friend, or Private.

You can define properties in classes, structures, and modules. Properties are Public by default, which means you can call them from anywhere in your application.

Argument Declaration

You declare each argument the same as for a Function or Sub procedure, except that the passing mechanism must be by value.

The syntax for each argument in the argument list is as follows:

[Optional] ByVal [ParamArray] argumentname As datatype

If the argument is optional, you must also supply a default value in its declaration, as follows:

Optional ByVal argumentname As datatype = defaultvalue

In a Get procedure, the return value is supplied to the calling expression as the value of the property.

In a Set procedure, the new property value is passed through the argument of the Set statement. If you declare an explicit argument, you must declare it as the same data type as the property. If you do not declare an argument, the compiler uses the implicit argument Value to represent the new value to be assigned to the property.

Calling Syntax

A Property procedure is invoked implicitly by code that makes reference to the property. The code uses the name of the property the same way it would use the name of a variable, except that it must provide values for all arguments that are not optional, and it must enclose the argument list in parentheses. If no arguments are supplied, you can optionally omit the parentheses.

The syntax for an implicit call to a Set Property procedure is as follows:

propertyname[(argumentlist)] = expression

The following sample calls show allowable calling syntax for a Get Property procedure:

lvalue = propertyname[(argumentlist)]
Do While (propertyname[(argumentlist)] > expression)
Note   A property's data type and access are defined in the Property statement, not in the Property procedures. A property can have only one data type and one accessibility. For example, you cannot define a property to set a Decimal value, but get a Double value. Similarly, you cannot define a Private Set and a Public Get on the same property. To achieve this functionality, you can define a Public ReadOnly property and a Private setting method separate from the property.

See Also

Procedure Arguments | Property Statement

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft