Home

The Methods of a Class

 

Introduction

The variables of a program cannot perform assignments. In the same way, the member variables of a class can initiate nor 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 also called a method. Throughout these lessons, the word method will be used as a procedure (Sub or Function) that is a member of a class.

 

Creating Methods

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:

Imports System

Module Exercise

    Class TRectangle
        Public Length As Double
        Public Height As Double

        Function Perimeter() As Double

        End Function
    End Class

    Sub Main()
        
    End Sub

End Module

A method can also be created not to return a value, making a sub procedure.

After declaring a procedure, in its body, you can implement the expected behavior. 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. Like the member variables, the methods can be accessed outside of the class using the period operator. Here is an example:

Imports System

Module Exercise

    Class TRectangle
        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

    Sub Main()
        Dim Recto As New TRectangle
        Dim Perimeter As Double
        Dim Area As Double

        Console.Write("Enter Rectangle Length: ")
        Recto.Length = Console.ReadLine()
        Console.Write("Enter Rectangle Height: ")
        Recto.Height = Console.ReadLine

        Console.WriteLine()
        Console.WriteLine("Rectangle Characteristics")
        Console.WriteLine("Length:    {0}", Recto.Length)
        Console.WriteLine("Height:    {0}", Recto.Height)
        Console.WriteLine("Perimeter: {0}", Recto.Perimeter)
        Console.WriteLine("Area:      {0}", Recto.Area)

        Console.WriteLine()
    End Sub


End Module
 

Practical LearningPractical Learning: Creating Methods of a Class

  1. To add two methods of the class, change the file as follows:
     
    Imports System
    
    Module Exercise
    
        Public Class DepartmentStore
            Public ItemNumber As Long
            Public ItemName As String
            Public UnitPrice As Double
    
            Public Sub RegisterStoreItem()
                Console.Write("Enter Item #:     ")
                ItemNumber = CLng(Console.ReadLine())
                Console.Write("Enter Item Name:  ")
                ItemName = Console.ReadLine()
                Console.Write("Enter Unit Price: $")
                UnitPrice = CDbl(Console.ReadLine())
            End Sub
    
            Public Sub ShowItem()
                Console.WriteLine("Item #:     {0}", ItemNumber)
                Console.WriteLine("Item Name:  {0}", ItemName)
                Console.WriteLine("Unit Price: {0}", UnitPrice)
            End Sub
        End Class
    
        Sub Main()
            Dim dptStore As DepartmentStore = New DepartmentStore
    
            Console.WriteLine(" =#=  Department Store =#=")
            Console.WriteLine(" --- Item Registration ---")
            Console.WriteLine("Enter the following pieces of information")
            dptStore.RegisterStoreItem()
    
            Console.WriteLine()
            Console.WriteLine(" =#= Department Store =#=")
            Console.WriteLine(" ---  Store Inventory ---")
            dptStore.ShowItem()
        End Sub
    
    End Module
  2. Save the file and switch to the Command Prompt
  3. Compile and execute the program. Here is an example of running it:
     
     
  4. Return to Notepad
 

Methods and Arguments

Like regular procedures we have used so far, methods of a class 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.

 

Practical LearningPractical Learning: Creating Argumentative Methods

  1. To create a method that takes arguments, change the file as follows:
     
    Imports System
    
    Module Exercise
    
        Class DepartmentStore
            Public ItemNumber As Long
            Public ItemName As String
            Public UnitPrice As Double
    
            Public Sub RegisterStoreItem(ByVal nbr As Long, _
    			ByVal name As String, ByVal price As Double)
                ItemNumber = nbr
                ItemName = name
                UnitPrice = price
            End Sub
    
            Public Sub ShowItem()
                Console.WriteLine("Item #:     {0}", ItemNumber)
                Console.WriteLine("Item Name:  {0}", ItemName)
                Console.WriteLine("Unit Price: {0}", UnitPrice)
            End Sub
        End Class
    
        Sub Main()
            Dim dptStore As DepartmentStore = New DepartmentStore
    
            Dim itmNbr As Long
            Dim itmName As String
            Dim uPrice As Double
    
            Console.Write("Enter Item #:     ")
            itmNbr = CLng(Console.ReadLine())
            Console.Write("Enter Item Name:  ")
            itmName = Console.ReadLine()
            Console.Write("Enter Unit Price: $")
            uPrice = CDbl(Console.ReadLine())
    
            dptStore.RegisterStoreItem(itmNbr, itmName, uPrice)
            dptStore.ShowItem()
        End Sub
    
    End Module
  2. Save the file and switch to the Command Prompt
  3. Compile and execute the application to test it
  4. Return to Notepad
 

Methods Overloading

Just done for regular procedures, the methods of a class can be overloaded. The difference between two overloaded methods must lie on their signature which includes the names of the methods, their arguments, and the numbers of their arguments.

 
 

Previous Copyright © 2004-2011 FunctionX Next