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.

Imagine you want to write a (console-based) program
for a department store and the customer has given you a preliminary
catalog as follows:

Stock #: 437876
Crinkled Georgette Dress
Unit: $42.95

Stock #: 790475
New Wool Comfort Pants
$38.75

Stock #: 740797
Pencil Skirt
$25.75

Stock: 608432
Silver 1/10-ct. T.W. Diamond Heart-Link Bracelet
$95.85

Stock #: 759470
Multistriped Organic Cotton Dress Shirt
$35.50

Stock #: 487046
Modest Dress Heels
$35.50

Each item in this catalog is represented by its Stock
number, its name or description, and its price. Based on this, you can
create a class that represents each item.

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.
Here is an example:

Notice that the Length and the Height member variables are
accessed inside of the Show() method without an instance of the Rectangle class.

Details on Implementing Methods

The Case of Function Methods

So far, we have seen how to create sub procedure in a class, which 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:

Friend Class Rectangle
Public Length As Double
Public Height As Double
Function Assign() As Double
End Function
End Class

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:

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

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:

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:

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

Any or both of these two rules must be respected.

Me

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:

Public Class Triangle
Public Base As Double
Public Height As Double
Public Area As Double
Public Sub Display()
Dim Area As Double
Area = Base * Height / 2
End Sub
End Class

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:

Public Module Exercise
Public Class Triangle
Public Base As Double
Public Height As Double
Public Area As Double
Public Sub Display()
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
MsgBox("Triangle Characteristics" & vbCrLf & _
"Base:" & vbTab & Me.Base & vbCrLf & _
"Height:" & vbTab & Me.Height & vbCrLf & _
"Area: " & vbTab & Area) ' Area is not a member of the Exercise class
End Sub
End Class
Public Function Main() As Integer
Dim tri As Triangle = New Triangle
tri.Display()
Return 0
End Function
End Module

This would produce:

There are rules you must follow when using Me:

Me can never be declared: it is automatically implied
when you create a class

Me cannot be used in a class A to access a member of class B

Me cannot be used in a Shared method. The following program will not
compile because Me is used in the Display() method declared as a Shared
method:

Public Class Triangle
Public Base As Double
Public Height As Double
Public Area As Double
Public Shared Sub Display()
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
MsgBox("Triangle Characteristics" & vbCrLf & _
"Base:" & vbTab & Me.Base & vbCrLf & _
"Height:" & vbTab & Me.Height & vbCrLf & _
"Area: " & vbTab & Area)
End Sub
End Class