Home

The Shared Members of a Class

 
 

Shared Member Variables

 

When you declare a variable of a class, you are said to have created an instance of the class. This makes an object available so you can initialize and use its member variables and methods as you see fit. Just as you can declare one instance of a class, in the same way, you can declare as many instances as you judge it necessary.

After declaring various variables of the same class and initializing them, each variable has and keeps its own values. Here is an example:

Public Module Exercise
    Friend Class Rectangle
        Public Length As Double
        Public Height As Double

        Public Function CalculatePerimeter#()
            Return (Length + Height) * 2
        End Function

        Public Function CalcaulteArea#()
            Return Length * Height
        End Function
    End Class

    Public Function Main() As Integer
        Dim RegistrationCard As New Rectangle
        Dim Invoice As New Rectangle

        RegistrationCard.Length = 24.55 : RegistrationCard.Height = 20.68
        Invoice.Length = 8.5 : Invoice.Height = 11.65

        MsgBox("==========================" & vbCrLf & _
               "=-= Paper Registration Characteristics =-=" & vbCrLf & _
               "Length: " & vbTab & vbTab & RegistrationCard.Length & vbCrLf & _
               "Height: " & vbTab & vbTab & RegistrationCard.Height & vbCrLf & _
               "Perimeter: " & vbTab & _
	       RegistrationCard.CalculatePerimeter#() & vbCrLf & _
               "Area: " & vbTab & vbTab & _
	       RegistrationCard.CalcaulteArea#() & vbCrLf & _
               "----------------------------------------------------" & vbCrLf & _
               "=-= Paper Registration Characteristics =-=" & vbCrLf & _
               "Length: " & vbTab & vbTab & Invoice.Length & vbCrLf & _
               "Height: " & vbTab & vbTab & Invoice.Height & vbCrLf & _
               "Perimeter: " & vbTab & Invoice.CalculatePerimeter#() & vbCrLf & _
               "Area: " & vbTab & vbTab & Invoice.CalcaulteArea#() & vbCrLf & _
               "==========================")
        Return 0
    End Function

End Module

This would produce:

Introduction to Shared Members

In order to access the member variables of the above Rectangle class, you must declare a variable of the class. The Visual Basic language provides an alternative. You can declare a member variable so that you can access it from anywhere in your code without primarily declaring a variable of the class. In order to have such a member variable, you must explicitly create it as shared.

To create a shared member variable, type the Shared keyword on its left when declaring it. After the shared member variable has been declared, you can use it like any other member variable of that class, except that you don't need to declare an instance of that class when you want to access that member variable. Here is an example:

Public Module Exercise
    Friend Class Rectangle
        Public Length As Double
        Public Shared Height As Double

        Public Function CalculatePerimeter#()
            Return (Length + Height) * 2
        End Function

        Public Function CalcaulteArea#()
            Return Length * Height
        End Function
    End Class

    Public Function Main() As Integer
        REM Notice that a Rectangle variable is not declared
        Rectangle.Height = 20.68

        MsgBox("Rectangle Characteristics" & vbCrLf & _
               "Height:" & vbTab & vbTab & Rectangle.Height)

        Return 0
    End Function
End Module

This would produce:

A Shared Member Variable

Based on this, when creating a class, you will decide whether you want a particular member variable to be shared or not. You can have only one, two, more, or all member variables shared in a class. Experience and your own goal will guide you.

 
 
 

Shared Methods

Consider the following class:

Public Module Exercise
    Friend Class Rectangle
        Public Shared Length As Double
        Public Shared Height As Double

        Public Function CalculatePerimeter#()
            Return (Length + Height) * 2
        End Function

        Public Function CalcaulteArea#()
            Return Length * Height
        End Function
    End Class

    Public Function Main() As Integer
        REM Notice that a Rectangle variable is not declared
        Rectangle.Height = 20.68
        Rectangle.Length = 32.47

        MsgBox("Rectangle Characteristics" & vbCrLf & _
               "Length:" & vbTab & Rectangle.Length & vbCrLf & _
               "Height:" & vbTab & Rectangle.Height)
        Return 0
    End Function

End Module

This would produce:

Shared

Like member variables, a method can be shared among classes. In some cases, shared methods are more used than shared member variables because a shared method allows performing an action on a class without declaring an instance of that class. To create a shared method, type the Shared keyword on the left of the Sub or the Function keyword. Here is an example:

Here is an example:

Friend Class Rectangle
    Shared Function CalculatePerimeter#()
        Return (Length + Height) * 2
    End Function
End Class

You can apply the access modifier on the method as we have done so far. Here are examples:

Friend Class Rectangle
    Public Shared Function CalculatePerimeter#()

    End Function

    Public Shared Function CalcaulteArea#()

    End Function
End Class

Like a shared member variable, once a method has been created as shared, it can be accessed directly from anywhere. Remember that you would need to type the name of the class before accessing the method. The name of the class allows you to "qualify" the method. Here is an example:

Public Module Exercise
    Friend Class Rectangle
        Public Shared Length As Double
        Public Shared Height As Double

        Public Shared Function CalculatePerimeter#()
            Return (Length + Height) * 2
        End Function

        Public Shared Function CalcaulteArea#()
            Return Length * Height
        End Function
    End Class

    Public Function Main() As Integer
        REM Notice that a Rectangle variable is not declared
        Rectangle.Height = 20.68
        Rectangle.Length = 32.47

        MsgBox("Rectangle Characteristics" & vbCrLf & _
               "Length:" & vbTab & vbTab & Rectangle.Length & vbCrLf & _
               "Height:" & vbTab & vbTab & Rectangle.Height & vbCrLf & _
               "Perimeter: " & vbTab & Rectangle.CalculatePerimeter#() & vbCrLf & _
               "Area: " & vbTab & vbTab & Rectangle.CalcaulteArea#())
        Return 0
    End Function

End Module
 
 
   
 

Home Copyright © 2009-2010 FunctionX, Inc.