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:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">

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

</script>

<title>Exercise</title>

</head>
<body>

<%
    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

    Response.Write("==========================" & "<br />" & _
               "=-= Paper Registration Characteristics =-=" & "<br />" & _
               "Length: " & vbTab & vbTab & RegistrationCard.Length & "<br />" & _
               "Height: " & vbTab & vbTab & RegistrationCard.Height & "<br />" & _
               "Perimeter: " & vbTab & _
	       RegistrationCard.CalculatePerimeter#() & "<br />" & _
               "Area: " & vbTab & vbTab & _
	       RegistrationCard.CalcaulteArea#() & "<br />" & _
               "----------------------------------------------------" & "<br />" & _
               "=-= Paper Registration Characteristics =-=" & "<br />" & _
               "Length: " & vbTab & vbTab & Invoice.Length & "<br />" & _
               "Height: " & vbTab & vbTab & Invoice.Height & "<br />" & _
               "Perimeter: " & vbTab & Invoice.CalculatePerimeter#() & "<br />" & _
               "Area: " & vbTab & vbTab & Invoice.CalcaulteArea#() & "<br />" & _
               "==========================")
%>

</body>
</html>

This would produce:

Class

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:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">

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

</script>

<title>Exercise</title>

</head>
<body>

<%
    REM Notice that a Rectangle variable is not declared
    Rectangle.Height = 20.68

    Response.Write("Rectangle Characteristics" & "<br />" & _
                   "Height:" & vbTab & vbTab & Rectangle.Height)
%>

</body>
</html>
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

        Response.Write("Rectangle Characteristics" & "<br />" & _
               "Height:" & vbTab & vbTab & Rectangle.Height)

        Return 0
    End Function
End Module

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:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">

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

</script>

<title>Exercise</title>

</head>
<body>

<%
    REM Notice that a Rectangle variable is not declared
    Rectangle.Height = 20.68
    Rectangle.Length = 32.47

    Response.Write("Rectangle Characteristics" & "<br />" & _
                   "Length:" & vbTab & Rectangle.Length & "<br />" & _
                   "Height:" & vbTab & Rectangle.Height)
%>

</body>
</html>

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:

<script language="vb" type="text/vb" runat="server">

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

</script>

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

<script language="vb" type="text/vb" runat="server">

Friend Class Rectangle
    Public Shared Function CalculatePerimeter#()

    End Function

    Public Shared Function CalcaulteArea#()

    End Function
End Class

</script>

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:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vb" type="text/vb" runat="server">

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

</script>

<title>Exercise</title>

</head>
<body>

<%
    REM Notice that a Rectangle variable is not declared
    Rectangle.Height = 20.68
    Rectangle.Length = 32.47

    Response.Write("Rectangle Characteristics" & "<br />" & _
               "Length:" & vbTab & vbTab & Rectangle.Length & "<br />" & _
               "Height:" & vbTab & vbTab & Rectangle.Height & "<br />" & _
               "Perimeter: " & vbTab & Rectangle.CalculatePerimeter#() & "<br />" & _
               "Area: " & vbTab & vbTab & Rectangle.CalcaulteArea#())
%>

</body>
</html>

This would produce:

Shared

Me

We have mentioned two techniques of accessing the members of a class, one consisted of declaring a variable of the class, the other had to do with Shared members. We know already that the members of a class are made available to all other members of the same class without being declared or qualified. Consider the following class:

<script language="vb" type="text/vb" runat="server">

Public Class Triangle
        Public Base As Double
        Public Height As Double
        Public Area As Double

        Public Sub Create()
            Dim Area As Double
            Area = Base * Height / 2
        End Sub
End Class

</script>

When the Area variable is used in the Display() method, there are two variables available and named Area. It makes it confusing to know what particular variable is being accessed. You can use a special member of a class that allows you to specify the member of a class when accessing it. This member is called Me.

When using Me, you can access any member of a class within any method of the same class. Here is an example:

<script language="vb" type="text/vb" runat="server">

Public Class Triangle
        Public Base As Double
        Public Height As Double
        Public Area As Double

        Public Sub Create()
            Dim Area As Double
            
            ' Using "this" to access the members of this class
            Me.Base = 24.55
            Me.Height = 20.75

            ' You cannot use this to access Area because Area 
            ' is not a member of this class
            Area = Me.Base * Me.Height / 2
        End Sub
End Class

</script>

There are rules you must follow when using Me:

  • Me can never be declared: it is automatically implied when you create a class
  • Me cannot be used in a class A to access a member of class B 
  • Me cannot be used in a Shared method
 
 
   
 

Previous Copyright © 2009-2010 FunctionX Next