Property StatementProperty Statement

宣告屬性的名稱,以及用來儲存和取出屬性值的屬性程式。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 ]
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:

    請參閱 Access levels in Visual BasicSee Access levels in Visual Basic.

  • propertymodifiers

    選擇性。Optional. 可以是下列其中一項:Can be one of the following:

  • Shared

    選擇性。Optional. 請參閱共用See Shared.

  • Shadows

    選擇性。Optional. 請參閱ShadowsSee Shadows.

  • ReadOnly

    選擇性。Optional. 請參閱ReadOnlySee ReadOnly.

  • WriteOnly

    選擇性。Optional. 請參閱WriteOnlySee WriteOnly.

  • Iterator

    選擇性。Optional. 請參閱IteratorSee Iterator.

  • name

    必要。Required. 屬性的名稱。Name of the property. 請參閱 Declared Element NamesSee Declared Element Names.

  • 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 語句See Implements Statement.

  • 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.

您只能在類別層級使用 PropertyYou 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. 您可以使用 Property 語句上的存取修飾詞來調整屬性的存取層級,也可以選擇性地將其中一個屬性程式調整為更嚴格的存取層級。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 在屬性指派期間,將參數傳遞給 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 而不指派值給 nameGet 程式會傳回屬性之資料類型的預設值。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

下列範例會在類別中宣告屬性。The following example declares a property in a class.

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

另請參閱See also