Shared (Visual Basic)Shared (Visual Basic)

指定一或多個宣告的程式設計專案與大型的類別或結構相關聯,而不是與類別或結構的特定實例產生關聯。Specifies that one or more declared programming elements are associated with a class or structure at large, and not with a specific instance of the class or structure.


使用 Shared 的時機When to Use Shared

共用類別或結構的成員,可讓每個實例(而不是共用)使用,而每個實例會保留自己的複本。Sharing a member of a class or structure makes it available to every instance, rather than nonshared, where each instance keeps its own copy. 例如,如果變數的值適用于整個應用程式,這就很有用。This is useful, for example, if the value of a variable applies to the entire application. 如果您將該變數宣告為 Shared,則所有實例都會存取相同的儲存位置,而如果一個實例變更變數的值,所有實例都會存取更新的值。If you declare that variable to be Shared, then all instances access the same storage location, and if one instance changes the variable's value, all instances access the updated value.

共用不會改變成員的存取層級。Sharing does not alter the access level of a member. 例如,類別成員可以是共用和私用(只能從類別記憶體取),或非共用和公用。For example, a class member can be shared and private (accessible only from within the class), or nonshared and public. 如需詳細資訊,請參閱Visual Basic 中的存取層級For more information, see Access levels in Visual Basic.


  • 宣告內容。Declaration Context. 您只能在模組層級使用 SharedYou can use Shared only at module level. 這表示 Shared 元素的宣告內容必須是類別或結構,而且不能是原始程式檔、命名空間或程式。This means the declaration context for a Shared element must be a class or structure, and cannot be a source file, namespace, or procedure.

  • 合併的修飾詞。Combined Modifiers. 您不能在相同的宣告中,同時使用覆寫、可覆寫、 NotOverridableMustOverride靜態來指定 SharedYou cannot specify Shared together with Overrides, Overridable, NotOverridable, MustOverride, or Static in the same declaration.

  • 正在.Accessing. 您可以存取共用專案,方法是以其類別或結構名稱加以限定,而不是使用其類別或結構之特定實例的變數名稱。You access a shared element by qualifying it with its class or structure name, not with the variable name of a specific instance of its class or structure. 您甚至不需要建立類別或結構的實例來存取其共用成員。You do not even have to create an instance of a class or structure to access its shared members.

    下列範例會呼叫共用程式 IsNaNDouble 結構所公開。The following example calls the shared procedure IsNaN exposed by the Double structure.

    If Double.IsNaN(result) Then MsgBox("Result is mathematically undefined.")

  • 隱含共用。Implicit Sharing. 您不能在Const 語句中使用 Shared 修飾詞,但會隱含地共用常數。You cannot use the Shared modifier in a Const Statement, but constants are implicitly shared. 同樣地,您無法將模組或介面的成員宣告為 Shared,但它們會隱含共用。Similarly, you cannot declare a member of a module or an interface to be Shared, but they are implicitly shared.


  • 容量.Storage. 共用變數或事件只會儲存一次,無論您建立類別或結構的實例有多少或數個。A shared variable or event is stored in memory only once, no matter how many or few instances you create of its class or structure. 同樣地,共用程式或屬性只會保留一組本機變數。Similarly, a shared procedure or property holds only one set of local variables.

  • 透過執行個體變數存取。Accessing through an Instance Variable. 藉由使用包含其類別或結構之特定實例的變數名稱來限定共用專案,可以存取該元素。It is possible to access a shared element by qualifying it with the name of a variable that contains a specific instance of its class or structure. 雖然這通常會如預期般運作,但編譯器會產生警告訊息,並透過類別或結構名稱而不是變數來進行存取。Although this usually works as expected, the compiler generates a warning message and makes the access through the class or structure name instead of the variable.

  • 透過實例運算式來存取。Accessing through an Instance Expression. 如果您透過傳回其類別或結構實例的運算式來存取共用專案,編譯器會透過類別或結構名稱進行存取,而不會評估運算式。If you access a shared element through an expression that returns an instance of its class or structure, the compiler makes the access through the class or structure name instead of evaluating the expression. 如果您想要運算式來執行其他動作以及傳回實例,這會產生非預期的結果。This produces unexpected results if you intended the expression to perform other actions as well as returning the instance. 下列範例將說明這點。The following example illustrates this.

    Sub main()  = 10  
        ' The preceding line is the preferred way to access total.  
        Dim instanceVar As New shareTotal  += 100  
        ' The preceding line generates a compiler warning message and  
        ' accesses total through class shareTotal instead of through  
        ' the variable instanceVar. This works as expected and adds  
        ' 100 to total.  
        returnClass().total += 1000  
        ' The preceding line generates a compiler warning message and  
        ' accesses total through class shareTotal instead of calling  
        ' returnClass(). This adds 1000 to total but does not work as  
        ' expected, because the MsgBox in returnClass() does not run.  
        MsgBox("Value of total is " & CStr(  
    End Sub  
    Public Function returnClass() As shareTotal  
        MsgBox("Function returnClass() called")  
        Return New shareTotal  
    End Function  
    Public Class shareTotal  
        Public Shared total As Integer  
    End Class  

    在上述範例中,編譯器會同時產生一則警告訊息,這兩次程式碼都會透過實例來存取共用變數 totalIn the preceding example, the compiler generates a warning message both times the code accesses the shared variable total through an instance. 在每個案例中,它會直接透過類別 shareTotal 存取,而不會使用任何實例。In each case it makes the access directly through the class shareTotal and does not make use of any instance. 在對程式 returnClass的預期呼叫中,這表示它甚至不會產生 returnClass的呼叫,因此不會執行顯示 "Function returnClass ()的其他動作。In the case of the intended call to the procedure returnClass, this means it does not even generate a call to returnClass, so the additional action of displaying "Function returnClass() called" is not performed.

Shared 修飾詞可用於以下內容:The Shared modifier can be used in these contexts:

Dim 陳述式Dim Statement

Event 陳述式Event Statement

Function 陳述式Function Statement

Operator StatementOperator Statement

Property 陳述式Property Statement

Sub 陳述式Sub Statement

請參閱See also