So far, we have seen how to create a sub procedure in a class. Remember that a sub procedure is a method that does not return a value. Like a normal procedure, a method can be made to return a value, in which case it would be a function. To create a method as a function, use the same techniques we have used so far to create a function. Here is an example: <script language="vb" type="text/vb" runat="server"> Friend Class Rectangle Public Length As Double Public Height As Double Function Assign() As Double End Function End Class </script> After declaring a procedure, in its body, you can implement the expected behavior. As seen for a sub procedure, when a method has been created, it has access to all of the other members of the same class. This means that you don't have to re-declare a member of a class to access it in a method. Based on this, you can manipulate any member variable of the same class as you wish. This means that, naturally, you do not have to create a function method to change the value of a member variable. A normal sub procedure can take care of this. Instead, you would create a method if you need to perform a calculation and get a value from it instead of storing that value in one of the member variables. Here is an example: <script language="vb" type="text/vb" runat="server"> Friend Class Rectangle Public Length As Double Public Height As Double Function Assign() As Double End Function Function Perimeter() As Double End Function End Class </script> Therefore, in the body of the function, you can access a member variable, you can call another method of the same class, or you can use an external value as you see fit. When the function exits, make sure it returns the appropriate value based on its type. Like the member variables, the methods can be accessed outside of the class using the period operator. 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 Function Perimeter() As Double Return (Length + Height) * 2 End Function Function Area#() Return Length * Height End Function End Class </script> <title>Exercise</title> </head> <body> <% Dim Recto As Rectangle = New Rectangle Recto.Length = 42.58 : Recto.Height = 28.08 Response.Write("=-= Rectangle Characteristics =-=" & "<br />" & _ "Length: " & vbTab & vbTab & Recto.Length & "<br />" & _ "Height: " & vbTab & vbTab & Recto.Height & "<br />" & _ "Perimeter: " & vbTab & Recto.Perimeter() & "<br />" & _ "Area: " & vbTab & vbTab & Recto.Area()) %> </body> </html> This would produce: Like a sub procedure that is created as a method, a member function of a class can be made private, public, or friendly. It follows the exact same rules we reviewed early.
Like regular procedures we have used so far, methods can have arguments. The rules are the same as we have applied them so far. When you create a method, it has direct access to all regular members of its class. This means that you don't have to create an argument for a member variable of the same class. You would need an argument only if an external value would be passed to the method.
Just as done for regular procedures, a method of a class can be overloaded. To overload a method, create more than one method with the same name. The methods with same names must have different rules in their arguments:
Any or both of these two rules must be respected. |
|
|||||||||||
|