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.


何时使用共享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. 不能在同一声明中同时指定 Shared重写、可重写NotOverridableMustOverride静态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.

    下面的示例调用 IsNaN Double 结构公开的共享过程。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  

    在前面的示例中,编译器会在代码通过实例访问共享变量 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的调用,因此不会执行显示 "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