Previous section   Next section

5.7 Using Shared Members

The properties and methods of a class can be either instance members or shared members. Instance members are associated with instances of a type, while shared members are associated with the class, and not with any particular instance. Methods are instance methods unless you explicitly mark them with the keyword Shared.

The vast majority of methods will be instance methods. The semantics of an instance method are that you are taking an action on a specific object. From time to time, however, it is convenient to be able to invoke a method without having an instance of the class, and for that you will use a shared method.

You can access a shared member through the name of the class in which it is declared. For example, suppose you have a class named Button and have instantiated objects of that class named btnUpdate and btnDelete.

Suppose that the Button class has an instance method Draw( ) and a shared method GetButtonCount( ). The job of Draw( ) is to draw the current button; the job of GetButtonCount( ) is to return the number of buttons currently visible on the form.

You access an instance method through an instance of the class; that is, through an object:

btnUpdate.SomeMethod( )

You can access a shared method in the same way:

btnUpdate.GetButtonCount( )

You can also access a shared method through the class name (rather than through an instance):

Button.GetButtonCount( )

This allows you to access the shared method without having an instance of the class.

A common use of shared member variables, or fields, is to keep track of the number of instances/objects that currently exist for your class. In Example 5-8, you create a Cat class. The Cat class might be used in a pet-store simulation. For this example, the Cat class has been stripped to its absolute essentials. Analysis follows.

Example 5-8. Shared fields
Option Strict On
Imports System

Class Cat
   Private Shared instances As Integer = 0
   Private weight As Integer
   Private name As String

   Public Sub New(ByVal name As String, ByVal weight As Integer)
      instances += 1
      Me.name = name
      Me.weight = weight
   End Sub

   Public Shared Sub HowManyCats( )
      Console.WriteLine("{0} cats adopted", instances)
   End Sub

   Public Sub TellWeight( )
      Console.WriteLine("{0} is {1} pounds", _
      name, weight)
   End Sub

End Class 'Cat

Module Module1

   Sub Main( )
      Cat.HowManyCats( )
      Dim frisky As New Cat("Frisky", 5)
      frisky.TellWeight( )
      Cat.HowManyCats( )
      Dim whiskers As New Cat("Whiskers", 7)
      whiskers.TellWeight( )  ' instance method
      whiskers.HowManyCats( ) ' shared method through instance
      Cat.HowManyCats( )      ' shared method through class name
   End Sub

End Module


Output:
0 cats adopted
Frisky is 5 pounds
1 cats adopted
Whiskers is 7 pounds
2 cats adopted
2 cats adopted

The Cat class begins by defining a shared member variable, instances, that is initialized to zero. This shared member field will keep track of the number of Cat objects created. Each time the constructor (Sub New) runs (creating a new object), the instances field is incremented.

The Cat class also defines two instance fields: name and weight. These track the name and weight of each individual Cat object.

The Cat class defines two methods: HowManyCats( ) and TellWeight( ). HowManyCats( ) is shared. The number of cats is not an attribute of any given Cat, it is an attribute of the entire class. TellWeight( ) is an instance method. The name and weight of each cat is per instance (i.e., each Cat has his own name and weight).

The Main( ) method accesses the shared HowManyCats( ) method directly, through the class:

Cat.HowManyCats( )

Main( ) then creates an instance of Cat and accesses the instance method TellWeight( ) through an instance (frisky) of Cat:

Dim frisky As New Cat("Frisky", 5)
frisky.TellWeight( )

Each time a new Cat is created, HowManyCats( ) reports the increase.

You access the instance method through the object, but you can access the shared method either through an object or through the class name:

whiskers.TellWeight( )
whiskers.HowManyCats( )
Cat.HowManyCats( )

  Previous section   Next section
Top