Partilhar via


Compartilhamento (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.

Comentários

When to Use Shared

Compartilhamento de um membro de uma classe ou estrutura disponibiliza para cada instância, em vez de não compartilhados, onde cada instância mantém sua própria cópia. This is useful, for example, if the value of a variable applies to the entire application. 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. For more information, see Níveis de acesso em Visual Basic.

Rules

  • Declaration Context. Você pode usar Shared somente no nível de módulo . 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. Não é possível especificar Shared em conjunto com Overrides (Visual Basic), Overridable (Visual Basic), NotOverridable (Visual Basic), MustOverride (Visual Basic), ou Static (Visual Basic) na mesma declaração.

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

    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. Não é possível usar o Shared modificador em um Declaração Const (Visual Basic), mas constantes são compartilhadas implicitamente. Similarly, you cannot declare a member of a module or an interface to be Shared, but they are implicitly shared.

Behavior

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

    In the preceding example, the compiler generates a warning message both times the code accesses the shared variable total through an instance. In each case it makes the access directly through the class shareTotal and does not make use of any instance. 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.

The Shared modifier can be used in these contexts:

Dim Statement

Event Statement

Function Statement

Operator Statement

Property Statement

Sub Statement

Consulte também

Referência

Shadows (Visual Basic)

Static (Visual Basic)

Conceitos

Tempo de vida no Visual Basic

Procedimentos no Visual Basic

Outros recursos

Estruturas (Visual Basic)

Objetos e Classes no Visual Basic