Defining Shared Class Members                                              
         
At times, you may wish to have a field value shared across all instances of your class. The typical (non-shared)
field is allocated per object; one object’s value is distinct from another object’s value. These are called
instance
fields
. To define a field that is allocated once and shared across all objects, use the Shared modifier. Do know
that the C++, C# and Java languages refer to this notion as 'static' fields.

Consider a class that represents a savings account. The account balance is an instance field, because each savings
account object needs a distinct balance. On the other hand, the interest rate is a shared field. If the fed changes
the rate, we (the banker) want it to be reflected in all savings account instances.


Public Class SavingsAccount
   Private mBalance As Double
   Private Shared mInterestRate As Double = 0.04
 
   Sub New(ByVal balance As Double)
      mBalance = balance
   End Sub
 
   Public Function GetEarnedInterest() As Double
      Return mBalance * mInterestRate
   End Function
 
   Public Property InterestRate() As Double
      Get
         Return mInterestRate
      End Get
      Set(ByVal Value As Double)
         mInterestRate = Value
      End Set
   End Property
End Class


Let’s look at what happens when the following code executes:


Sub Main()     
  Dim s1 As SavingsAccount = New SavingsAccount(50)
  Dim s2 As SavingsAccount = New SavingsAccount(100)
  Dim s3 As SavingsAccount = New SavingsAccount(10000.75)
End Sub


















Let’s look at another code example:


Sub Main()
   Dim s1 As SavingsAccount = New SavingsAccount(50)
   Dim s2 As SavingsAccount = New SavingsAccount(100)
    
   'Changing the interest rate.
   s2.InterestRate = 0.08
   Console.WriteLine("Current rate is " & s1.InterestRate)
    
   'Create another account. Does rate get set back to .04?
   Dim s3 As SavingsAccount = New SavingsAccount(10000.75)
   Console.WriteLine("Current rate is " & s1.InterestRate)
End Sub


The output demonstrates that the runtime only initializes the shared field once, not each time an instance is
created. The runtime guarantees that a shared field will be initialized sometime before it is first accessed.










Defining Shared Constructors     
                                                          
You can also implement custom shared field initialization logic by providing a shared constructor within the
class. A shared constructor must not have any access modifiers and cannot take any parameters. The runtime
invokes the shared constructor when it creates an instance of the class or before accessing a shared member.

The shared constructor executes before any instance constructors. This is a perfect place to assign static data
from an external source (such as a database or file on the hard drive). Also, this gives you a place to perform any
special class-level start up logic (such as writing to an event log). Here, we will simply call Console.WriteLine()
and assign our shared field.


Public Class SavingsAccount
   Private mBalance As Double
   Private Shared mInterestRate As Double
'Set to default value of 0

   'Shared ctor! Note: no access modifier, no params
   Shared Sub New()
     'Implement custom shared initialization logic
     Console.WriteLine("In shared constructor!")
     mInterestRate = 0.04
   End Sub
 
   Public Sub New(ByVal balance As Double)
      Console.WriteLine("In instance constructor!")
      mBalance = balance
   End Sub
...
End Class

For example, here’s some consuming code and the output:

Sub Main()
  Dim s1 as SavingsAccount= New SavingsAccount(50)
  Console.WriteLine("Current rate is {0}", s1.InterestRate)
End Sub











Defining Shared Methods and Properties             
                          
In addition to shared fields and constructors, you can also mark methods and properties as shared. A shared
method or property can only access other shared members. If the shared member is not private, then users can
access it using the class name or a class instance.
For example, let’s make the InterestRate property shared:


Public Class SavingsAccount
...
   Public Shared Property InterestRate() As Double
      Get
         Return mInterestRate
      End Get
      Set(ByVal Value As Double)
         mInterestRate = Value
      End Set
   End Property
End Class


Now we can call this property via the class name or an object:


Sub Main()     
   'Change the current rate. NOTE: No SavingsAccount object
   'has been created yet!
   SavingsAccount.InterestRate = 0.1 ' <-- shared ctor called here
    
   Dim s1 As SavingsAccount = New SavingsAccount(50)
   Dim s2 As SavingsAccount = New SavingsAccount(100)
    
   Console.WriteLine("Current rate is " & SavingsAccount.InterestRate)
   Console.WriteLine("Current rate is " & s1.InterestRate)
End Sub
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Shared Class Members, Constructors, Properties
Table of Contents
Courseware
Training Resources
Tutorials
Services