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