Property Statement

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

1/25/2017 PropertyStatement

Property Statement
Visual Studio 2015

Updated: July 20, 2015

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

Declares the name of a property, and the property procedures used to store and retrieve the value of the property.

Syntax

[<attributelist>][Default][accessmodifier]
[propertymodifiers][Shared][Shadows][ReadOnly|WriteOnly][Iterator]
Propertyname([parameterlist])[Asreturntype][Implementsimplementslist]
[<attributelist>][accessmodifier]Get
[statements]
EndGet
[<attributelist>][accessmodifier]Set(ByValvalueAsreturntype[,parameterlist])
[statements]
EndSet
EndProperty
or
[<attributelist>][Default][accessmodifier]
[propertymodifiers][Shared][Shadows][ReadOnly|WriteOnly]
Propertyname([parameterlist])[Asreturntype][Implementsimplementslist]

Parts

https://msdn.microsoft.com/enus/library/zzh9ha57.aspx 1/8
1/25/2017 PropertyStatement

attributelist

Optional. List of attributes that apply to this property or Get or Set procedure. See Attribute List.

Default

Optional. Specifies that this property is the default property for the class or structure on which it is defined. Default properties must accept parameters and can be
set and retrieved without specifying the property name. If you declare the property as Default, you cannot use Private on the property or on either of its
property procedures.

accessmodifier

Optional on the Property statement and on at most one of the Get and Set statements. Can be one of the following:

Public

Protected

Friend

Private

ProtectedFriend

See Access Levels in Visual Basic.

propertymodifiers

Optional. Can be one of the following:

Overloads

Overrides

Overridable

NotOverridable

MustOverride

MustOverrideOverrides

NotOverridableOverrides

https://msdn.microsoft.com/enus/library/zzh9ha57.aspx 2/8
1/25/2017 PropertyStatement

Shared

Optional. See Shared.

Shadows

Optional. See Shadows.

ReadOnly

Optional. See ReadOnly.

WriteOnly

Optional. See WriteOnly.

Iterator

Optional. See Iterator.

name

Required. Name of the property. See Declared Element Names.

parameterlist

Optional. List of local variable names representing the parameters of this property, and possible additional parameters of the Set procedure. See Parameter List.

returntype

Required if Option``Strict is On. Data type of the value returned by this property.

Implements

Optional. Indicates that this property implements one or more properties, each one defined in an interface implemented by this property's containing class or
structure. See Implements Statement.

implementslist

Required if Implements is supplied. List of properties being implemented.

implementedproperty[,implementedproperty...]

https://msdn.microsoft.com/enus/library/zzh9ha57.aspx 3/8
1/25/2017 PropertyStatement

Each implementedproperty has the following syntax and parts:

interface.definedname

Part Description

interface Required. Name of an interface implemented by this property's containing class or structure.

definedname Required. Name by which the property is defined in interface.

Get

Optional. Required if the property is marked WriteOnly. Starts a Get property procedure that is used to return the value of the property.

statements

Optional. Block of statements to run within the Get or Set procedure.

EndGet

Terminates the Get property procedure.

Set

Optional. Required if the property is marked ReadOnly. Starts a Set property procedure that is used to store the value of the property.

EndSet

Terminates the Set property procedure.

EndProperty

Terminates the definition of this property.

Remarks
https://msdn.microsoft.com/enus/library/zzh9ha57.aspx 4/8
1/25/2017 PropertyStatement

The Property statement introduces the declaration of a property. A property can have a Get procedure read only, a Set procedure write only, or both readwrite. You
can omit the Get and Set procedure when using an autoimplemented property. For more information, see AutoImplemented Properties.

You can use Property only at class level. This means the declaration context for a property must be a class, structure, module, or interface, and cannot be a source file,
namespace, procedure, or block. For more information, see Declaration Contexts and Default Access Levels.

By default, properties use public access. You can adjust a property's access level with an access modifier on the Property statement, and you can optionally adjust one of
its property procedures to a more restrictive access level.

Visual Basic passes a parameter to the Set procedure during property assignments. If you do not supply a parameter for Set, the integrated development environment
IDE uses an implicit parameter named value. This parameter holds the value to be assigned to the property. You typically store this value in a private local variable and
return it whenever the Get procedure is called.

Rules

Mixed Access Levels. If you are defining a readwrite property, you can optionally specify a different access level for either the Get or the Set procedure, but not
both. If you do this, the procedure access level must be more restrictive than the property's access level. For example, if the property is declared Friend, you can
declare the Set procedure Private, but not Public.

If you are defining a ReadOnly or WriteOnly property, the single property procedure Get or Set, respectively represents all of the property. You cannot declare a
different access level for such a procedure, because that would set two access levels for the property.

Return Type. The Property statement can declare the data type of the value it returns. You can specify any data type or the name of an enumeration, structure,
class, or interface.

If you do not specify returntype, the property returns Object.

Implementation. If this property uses the Implements keyword, the containing class or structure must have an Implements statement immediately following its
Class or Structure statement. The Implements statement must include each interface specified in implementslist. However, the name by which an interface
defines the Property in definedname does not have to be the same as the name of this property in name.

Behavior

Returning from a Property Procedure. When the Get or Set procedure returns to the calling code, execution continues with the statement following the
statement that invoked it.
https://msdn.microsoft.com/enus/library/zzh9ha57.aspx 5/8
1/25/2017 PropertyStatement

The ExitProperty and Return statements cause an immediate exit from a property procedure. Any number of ExitProperty and Return statements can
appear anywhere in the procedure, and you can mix ExitProperty and Return statements.

Return Value. To return a value from a Get procedure, you can either assign the value to the property name or include it in a Return statement. The following
example assigns the return value to the property name quoteForTheDay and then uses the ExitProperty statement to return.

VB

PrivatequoteValueAsString="Noquoteassignedyet."

VB

ReadOnlyPropertyquoteForTheDay()AsString
Get
quoteForTheDay=quoteValue
ExitProperty
EndGet
EndProperty

If you use ExitProperty without assigning a value to name, the Get procedure returns the default value for the property's data type.

The Return statement at the same time assigns the Get procedure return value and exits the procedure. The following example shows this.

VB

PrivatequoteValueAsString="Noquoteassignedyet."

VB

ReadOnlyPropertyquoteForTheDay()AsString
Get
ReturnquoteValue

https://msdn.microsoft.com/enus/library/zzh9ha57.aspx 6/8
1/25/2017 PropertyStatement

EndGet
EndProperty

Example
The following example declares a property in a class.

VB

ClassClass1
'Definealocalvariabletostorethepropertyvalue.
PrivatepropertyValueAsString
'Definetheproperty.
PublicPropertyprop1()AsString
Get
'TheGetpropertyprocedureiscalledwhenthevalue
'ofapropertyisretrieved.
ReturnpropertyValue
EndGet
Set(ByValvalueAsString)
'TheSetpropertyprocedureiscalledwhenthevalue
'ofapropertyismodified.Thevaluetobeassigned
'ispassedintheargumenttoSet.
propertyValue=value
EndSet
EndProperty
EndClass

See Also
AutoImplemented Properties
Objects and Classes
Get Statement

https://msdn.microsoft.com/enus/library/zzh9ha57.aspx 7/8
1/25/2017 PropertyStatement

Set Statement
Parameter List
Default

2017 Microsoft

https://msdn.microsoft.com/enus/library/zzh9ha57.aspx 8/8

You might also like