Home

The Methods of a Class

 

Methods Fundamentals

 

Introduction

The variables of a program cannot perform assignments. In the same way, the member variables of a class cannot perform actions. So far, we were using procedures to do that. In the same way, to create a class as complete as possible, you can equip it with its own procedures. Such a procedure is created as a member of the class.

A procedure that is made a member of a class is called a method.

Creating a Method

To create a method, in the body of a class, use  the same formula we learned in Lesson 5. For example, to create a sub procedure, you use the following formula:

Class ClassName

    Sub ProcedureName()

    End Sub

End Class

Here is an example:

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

Private Class Rectangle

    Sub Show()
        
    End Sub

End Class

</script>

In the body of the method, do whatever you want.

Accessing a Method

To access a method outside of its class, you can use the period operator as we saw for the member variables. Here is an example

<%@ Page Language="VB" %>

<html>
<head>

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

Private Class Rectangle

    Sub Show()
        
    End Sub

End Class

</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim Recto As Rectangle

    Recto = New Rectangle

    Recto.Show()
%>

</body>
</html>

In the same way, you can use a With statement to access a method of a class.

Using the Member Variables of a Class

In the previous sections, we saw how to create member variables inside of a class. Here is an example:

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

Private Class Rectangle
    Length As Double
    Height As Double

    Sub Show()
        Response.Write("Whatever")
    End Sub

End Class

</script>

When you have created a member variable in a class, it is available to all methods of the same class. This means that, to use it from a method of the same class, you do not have to declare a variable for the class.

Class Members and Access Modifiers

 

The Private Member Variables of a Class

In our introduction to member variables of a class, we saw that they could be made friend or public. Here is an example:

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

Private Class Cylinder
    Friend Radius As Double
End Class

</script>

A member variable can also be made private. A member is private if it can be accessed only by other members of the same class. Other classes from the same module, the same file, the same project or outside of the project, cannot access such a member variable.

To declare a private member variable, precede it with the Private keyword. Here is an example:

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

Private Class Cylinder
    Friend Radius As Double
    Private Height As Double
End Class

</script>

As mentioned already, the private member variable is available to the methods of the same class and those methods can use it as they see fit. 

Access Modifiers and Methods

Because methods are normal members of a class, you can control their level of access. By default, when you create a method, if you do not specify its access modifier, it is automatically made public.

A method is referred to as public if it can be accessed by any class of the same module, any class of the same project, or an external class. To specify that a method is public, precede it with the Public keyword. 

You can also create a public method but confine it to only the classes of your project. Such a method is referred to as a friend.

Instead of exposing a method to any client of a class, you can restrict its access to only the fellow members of the same class. This is referred to as a private method. To create a private method, precede it with the Private keyword. Here is an example:

 
 
 
 

Details on Implementing Methods

 

The Case of Function Methods

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:

Method

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.

Methods and Arguments

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.

Methods Overloading

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:

  • They can have different types of arguments. Here is an example:
     
    <script language="vb" type="text/vb" runat="server">
    
    Private Class EmployeeInformation
        REM Contractor Registration
        Private Sub Register(ByVal SalaryCategory As Integer)
    
        End Sub
    
        REM Full Time Employee Registration
        Private Sub Register(ByVal HourlySalary As Double)
    
        End Sub
    End Class
    
    </script>
  • They can have a different number of arguments. Here is an example:
     
    <script language="vb" type="text/vb" runat="server">
    
    Private Class Geometry
        REM The area of a square
        Friend Function CalculateArea#(ByVal Side#)
            Area# = Side * Side
        End Function
    
        REM The area of a rectangle
        Friend Function CalculateArea#(ByVal Length#, ByVal Height#)
            Area# = Length# * Height#
        End Function
    End Class
    
    </script>

Any or both of these two rules must be respected.

 
 
   
 

Previous Copyright © 2009-2013 FunctionX, Inc. Next