共用方式為


Shared Members in Visual Basic 

Shared members are properties, procedures, and fields that are shared by all instances of a class or structure. Some programming languages refer to such items as static members.

Shared Fields and Properties

Shared fields and properties are useful when you have information that is part of a class but is not specific to any one instance of a class. When you change the value of a shared field and property, you change the value associated with the class and all instances of the class.

On the other hand, changing the value of a non-shared field or property associated with any one instance does not affect the value of that field or property in other instances of the class. Non-shared fields and properties exist independently for each instance of a class.

In this way, shared fields and properties behave like global variables that can be accessed only from instances of a class, or with qualification of the class name. Without shared fields and properties, you would need to use module-level variables to achieve the same effect. However, module-level variables can make your classes difficult to understand and maintain. Furthermore, using module-level variables in this way violates the concept of encapsulation that classes represent.

Shared Procedures

Shared procedures are class methods that are not associated with a specific instance of a class. For example, the Cos method defined within the Math class is a shared method. You can call a shared procedure as a method of an object or directly from the class.

Shared procedures and properties do not have access to instances of the class. For this reason, only qualified references to non-shared data members are allowed in shared methods.

Note

It is recommended that you do not write code in which you access a shared member through an instance of the class. This is because the compiler disregards the qualification of the shared member and treats it as if it were accessed directly through the class. In some situations, you may intend for the qualifying object to run some code, and therefore the Visual Basic compiler generates a warning for accessing a shared member through an instance of a class. IntelliSense does not display shared members for an instance of the class.

Shared Members Example

The following example creates a shared field, two instance fields, and a shared method to demonstrate how shared members operate in code:

Public Class Item
    Public Shared Count As Integer = 1
    Public Shared Sub ShareMethod()
        MsgBox("Current value of Count: " & Count)
    End Sub

    Public Sub New(ByVal Name As String)
        ' Use Count to initialize SerialNumber.
        Me.SerialNumber = Count
        Me.Name = Name
        ' Increment the shared variable
        Count += 1
    End Sub
    Public SerialNumber As Integer
    Public Name As String
    Public Sub InstanceMethod()
        MsgBox("Information in the first object: " & _
            Me.SerialNumber & vbTab & Me.Name)
    End Sub
End Class

Sub TestShared()
    ' Create two instances of the class.
    Dim part1 As New Item("keyboard")
    Dim part2 As New Item("monitor")

    part1.InstanceMethod()
    part2.InstanceMethod()
    Item.ShareMethod()
End Sub

When you execute the TestShared procedure, two instances of the class are created. The constructor uses the shared field Count to initialize the instance field SerialNumber, then increments Count. This technique automatically gives each instance a different serial number.

After creating the two instances, the instance method InstanceMethod is called on both objects, and the shared method ShareMethod is also called. The output is:

Information in the first object: 1 keyboard

Information in the second object: 2 monitor

Current value of the shared Count field: 3

See Also

Reference

Dim Statement (Visual Basic)
Cos

Concepts

Structures and Classes

Other Resources

Class Properties, Fields, and Methods
Structures: Your Own Data Types