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.

備註Remarks

何時使用共用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.

規則Rules

  • 宣告內容。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. 您無法指定Shared連同覆寫OverridableNotOverridableMustOverride,或靜態相同宣告中。You 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. 您無法使用Shared修飾詞Const 陳述式,但是常數會以隱含方式共用。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.

行為Behavior

  • 儲存體。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()  
        shareTotal.total = 10  
        ' The preceding line is the preferred way to access total.  
        Dim instanceVar As New shareTotal  
        instanceVar.total += 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(shareTotal.total))  
    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  
    

    在上述範例中,編譯器會產生一則警告訊息的程式碼會存取共用的變數這兩個時間total透過執行個體。In 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,因此不會執行其他動作,顯示 「 呼叫函式 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 陳述式Operator Statement

Property 陳述式Property Statement

Sub 陳述式Sub Statement

另請參閱See also