Export (0) Print
Expand All

Shared Members

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

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. Normal fields and properties exist independently for each instance of a class. Changing the value of a field or property associated with any one instance does not affect the value of fields or properties of other instances of the class. On the other hand, when you change the value of a shared field and property associated with an instance of a class, you change the value associated with all instances of the class. In this way, shared fields and properties behave like global variables that can be accessed only from instances of a class. Without static 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 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 by calling it either as a method of an object, or directly from the class. In this way, shared procedures represent an exception to the rule that you must create an instance of a class before you can use it.

Shared procedures are not implicitly passed instances of the class. For this reason, no unqualified references to non-shared data members are allowed in shared methods.

Shared Members Example

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

Public Class ShareClass
   Public InstanceValue As String
   Public Shared SharedValue As String
   Public Shared Sub ShareMethod()
      MsgBox("This is a shared method.") 
   End Sub
End Class
Sub TestShared()
   Dim Shared1 As New ShareClass()   ' Create an instance of the class.
   Dim Shared2 As New ShareClass()   ' Create an instance of the class.
   Shared1.SharedValue = "Share Value 1"    ' Set the value of a shared field.
   Shared2.SharedValue = "Share Value 2"   ' Overwrite the first value.
   MsgBox("The value of the shared field in the first instance" & _
      "is: " & Shared1.SharedValue)
   MsgBox("The value of the shared field in the second instance" & _
      "is: " & Shared2.SharedValue)
   ' Call a method on the class without creating an instance.
End Sub

When you execute the TestShared procedure, two instances of the class are created, and the shared field SharedValue is modified for both instances. When the shared field in the second instance of the class is modified, the value assigned to the shared field in the first instance of the class is overwritten because both instances refer to the same field.

For the sake of clarity, this example uses object variables to access shared members, but it is better programming practice to access shared members directly using the class name; for example, ShareClass.SharedValue = "Share Value 2". Using class syntax for shared members reduces confusion, because it makes it clear when you are using shared members and when you are using instance members.

See Also

Class Properties, Fields, and Methods | Structures: Your Own Data Types | Procedures | Structures and Classes | Dim Statement | Math.Cos Method

© 2014 Microsoft