# Property StatementProperty Statement

## 语法Syntax

[ <attributelist> ] [ Default ] [ accessmodifier ]
[ propertymodifiers ] [ Shared ] [ Shadows ] [ ReadOnly | WriteOnly ] [ Iterator ]
Property name ( [ parameterlist ] ) [ As returntype ] [ Implements implementslist ]
[ <attributelist> ] [ accessmodifier ] Get
[ statements ]
End Get
[ <attributelist> ] [ accessmodifier ] Set ( ByVal value As returntype [, parameterlist ] )
[ statements ]
End Set
End Property
- or -
[ <attributelist> ] [ Default ] [ accessmodifier ]
[ propertymodifiers ] [ Shared ] [ Shadows ] [ ReadOnly | WriteOnly ]
Property name ( [ parameterlist ] ) [ As returntype ] [ Implements implementslist ]


## 部件Parts

• attributelist

可选。Optional. 应用于此属性或 GetSet 过程的特性的列表。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. 如果将属性声明为 Default，则不能对属性或它的任何一个属性过程使用 PrivateIf you declare the property as Default, you cannot use Private on the property or on either of its property procedures.

• accessmodifier

Property 语句上以及最多一个 GetSet 语句上是可选的。Optional on the Property statement and on at most one of the Get and Set statements. 可以是以下各项之一：Can be one of the following:

• propertymodifiers

可选。Optional. 可以是以下各项之一：Can be one of the following:

• Shared

可选。Optional. 请参阅共享See Shared.

• Shadows

• ReadOnly

• WriteOnly

可选。Optional. 请参阅WriteOnlySee WriteOnly.

• Iterator

可选。Optional. 请参阅迭代器See Iterator.

• name

必需。Required. 属性的名称。Name of the property.

• parameterlist

可选。Optional. 表示此属性的参数以及 Set 过程可能的其他参数的局部变量名称列表。List of local variable names representing the parameters of this property, and possible additional parameters of the Set procedure. 请参阅参数列表See Parameter List.

• returntype

如果 Option Strict On，则为必需。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. 请参阅Implements 语句

• implementslist

如果提供 Implements，则是必需的。Required if Implements is supplied. 要实现的属性列表。List of properties being implemented.

implementedproperty [ , implementedproperty ... ]

每个 implementedproperty 都具有以下语法和部件：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. 用于在 interface中定义属性的名称。Name by which the property is defined in interface.
• Get

可选。Optional. 如果将属性标记 ReadOnly，则为必需。Required if the property is marked ReadOnly. 启动 Get 属性过程，该过程用于返回属性的值。Starts a Get property procedure that is used to return the value of the property. Get 语句不与自动实现的属性一起使用。The Get statement is not used with auto-implemented properties.

• statements

可选。Optional. 要在 GetSet 过程中运行的语句块。Block of statements to run within the Get or Set procedure.

• End Get

终止 Get 属性过程。Terminates the Get property procedure.

• Set

可选。Optional. 如果将属性标记 WriteOnly，则为必需。Required if the property is marked WriteOnly. 启动 Set 属性过程，该过程用于存储属性的值。Starts a Set property procedure that is used to store the value of the property. Set 语句不与自动实现的属性一起使用。The Set statement is not used with auto-implemented properties.

• End Set

终止 Set 属性过程。Terminates the Set property procedure.

• End Property

终止此属性的定义。Terminates the definition of this property.

## 备注Remarks

Property 语句引入属性的声明。The Property statement introduces the declaration of a property. 属性可以有 Get 过程（只读）、Set 过程（只写）或两者（读写）。A property can have a Get procedure (read only), a Set procedure (write only), or both (read-write). 使用自动实现的属性时，可以省略 GetSet 过程。You can omit the Get and Set procedure when using an auto-implemented property. 有关详细信息，请参阅自动实现的属性For more information, see Auto-Implemented Properties.

Visual Basic 在属性赋值期间将参数传递给 Set 过程。Visual Basic passes a parameter to the Set procedure during property assignments. 如果没有为 Set提供参数，集成开发环境（IDE）将使用名为 value的隐式参数。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. 通常将此值存储在私有本地变量中，并在调用 Get 过程时返回此值。You typically store this value in a private local variable and return it whenever the Get procedure is called.

## 规则Rules

• 混合访问级别。Mixed Access Levels. 如果要定义读写属性，则可以选择为 GetSet 过程指定不同的访问级别，但不能同时指定两者。If you are defining a read-write 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. 例如，如果属性已 Friend声明，则可以将 Set 过程声明 Private但不 PublicFor example, if the property is declared Friend, you can declare the Set procedure Private, but not Public.

如果要定义 ReadOnlyWriteOnly 属性，则单个属性过程（分别为GetSet）表示所有属性。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. Property 语句可以声明其返回的值的数据类型。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.

如果不指定 returntype，属性将返回 ObjectIf you do not specify returntype, the property returns Object.

• 部署.Implementation. 如果此属性使用 Implements 关键字，则包含类或结构必须紧跟在其 ClassStructure 语句后面的 Implements 语句。If this property uses the Implements keyword, the containing class or structure must have an Implements statement immediately following its Class or Structure statement. Implements 语句必须包括 implementslist中指定的每个接口。The Implements statement must include each interface specified in implementslist. 但是，接口定义 Property （在 definedname中）的名称不必与此属性的名称相同（在 name中）。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. GetSet 过程返回到调用代码时，执行将继续执行调用它的语句后面的语句。When the Get or Set procedure returns to the calling code, execution continues with the statement following the statement that invoked it.

Exit PropertyReturn 语句导致直接从属性过程退出。The Exit Property and Return statements cause an immediate exit from a property procedure. 任意数量的 Exit PropertyReturn 语句可以出现在过程中的任何位置，并且可以混合 Exit PropertyReturn 语句。Any number of Exit Property and Return statements can appear anywhere in the procedure, and you can mix Exit Property and Return statements.

• 返回值。Return Value. 若要从 Get 过程返回值，可将值分配给属性名称或将其包含在 Return 语句中。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. 下面的示例将返回值分配给属性名称 quoteForTheDay，然后使用 Exit Property 语句返回。The following example assigns the return value to the property name quoteForTheDay and then uses the Exit Property statement to return.

Private quoteValue As String = "No quote assigned yet."

ReadOnly Property QuoteForTheDay() As String
Get
QuoteForTheDay = quoteValue
Exit Property
End Get
End Property


如果使用 Exit Property 但未向 name赋值，Get 过程将返回属性数据类型的默认值。If you use Exit Property without assigning a value to name, the Get procedure returns the default value for the property's data type.

Return 语句同时分配 Get 过程返回值并退出该过程。The Return statement at the same time assigns the Get procedure return value and exits the procedure. 下面的示例演示了这一点。The following example shows this.

Private quoteValue As String = "No quote assigned yet."

ReadOnly Property QuoteForTheDay() As String
Get
Return quoteValue
End Get
End Property


## 示例Example

Class Class1
' Define a local variable to store the property value.
Private propertyValue As String
' Define the property.
Public Property Prop1() As String
Get
' The Get property procedure is called when the value
' of a property is retrieved.
Return propertyValue
End Get
Set(ByVal value As String)
' The Set property procedure is called when the value
' of a property is modified.  The value to be assigned
' is passed in the argument to Set.
propertyValue = value
End Set
End Property
End Class