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.
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
Learning: Creating Methods of a Class
|
|
- 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
|
- Save the file and switch to the Command Prompt
- Compile and execute the program. Here is an example of running it:
- Return to Notepad
|
|