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,您無法使用Private屬性上或在其中一個屬性程序。If 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. 請參閱迭代器See 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 StrictOnRequired 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

    如果使用 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. 屬性定義在名稱interfaceName by which the property is defined in interface.
  • Get

    選擇性。Optional. 所需屬性會標示為WriteOnlyRequired if the property is marked WriteOnly. 啟動Get屬性程序是用來傳回屬性的值。Starts a Get property procedure that is used to return the value of the property.

  • statements

    選擇性。Optional. 執行內的陳述式區塊GetSet程序。Block of statements to run within the Get or Set procedure.

  • End Get

    終止Get屬性程序。Terminates the Get property procedure.

  • Set

    選擇性。Optional. 所需屬性會標示為ReadOnlyRequired if the property is marked ReadOnly. 啟動Set屬性程序是用來儲存屬性的值。Starts a Set property procedure that is used to store the value of the property.

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

您可以使用Property只能在類別層級。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. 您可以調整屬性的存取層級的存取修飾詞在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) 會使用名為的隱含參數valueIf 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.

    如果您要定義ReadOnly或是WriteOnly屬性中,單一的屬性程序 (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關鍵字、 包含類別或結構必須Implements陳述式緊接著其ClassStructure陳述式。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陳述式必須包含在指定的每個介面implementslistThe 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

下列範例會宣告類別中的屬性。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