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.

The Shared Members of a Class

Shared Member Variables

When you declare a variable of a class, you are said to have
created an instance of the class. This makes an object available so you can
initialize and use its member variables and methods as you see fit. Just as you can declare one instance of a class, in the same
way, you can declare as many instances as you judge it necessary. After
declaring various variables of the same class and initializing them, each
variable has and keeps its own values. Here is an example:

In order to access the member variables of the above Rectangle
class, you must declare a variable of the class. The Visual Basic language provides an
alternative. You can declare a member variable so that you can access
it from anywhere in your code without primarily declaring a variable of the
class. In order to have such a member variable, you must explicitly create it as
shared.

To create a shared member
variable, type the Shared keyword on its left when declaring it. After
the shared member variable has been declared, you can use it like any other
member variable of that class, except that you don't need to declare an instance
of that class when you want to access that member variable. Here is an example:

Based on this, when creating a class, you will decide
whether you want a particular member variable to be shared or not. You can have
only one, two, more, or all member variables shared in a class. Experience and
your own goal will guide you.

Like member variables, a method can be shared among
classes. In some cases, shared methods are more used than shared member
variables because a shared method allows performing an action on a
class without declaring an instance of that class. To create a shared method, type the Shared keyword
on the left of the Sub or the Function keyword. Here is an
example:

Like a shared member variable, once a
method has been created as shared, it can be accessed directly from
anywhere. Remember that you would need to type the name of the class
before accessing the method. The name of the class allows you to
"qualify" the method. Here is an example:

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

Techniques of Initializing a Member Variable

Introduction

In Lesson 2, we saw that
when you declare a variable, the compiler initializes its allocated memory with
a default value. Consider the following program:

Notice that this program indicates that the Side member
variable of the Square class was initialized with 0. This means that, like the
regular variables, the member variables of a class are initialized by the
compiler with default values that depend on the type of the variable. For
example, a numeric member variable is initialized with 0 while a string-based
variable is initialized with an empty string.

After adding a member variable to a class, instead of
relying on the default value assigned by the compiler, you can initialized it
with a value of your choice, depending on the type of the variable. You have
various alternatives.

Practical
Learning: Creating a Class

Create a Console Application named
DepartmentStore3

In the Solution Explorer, right-click Module1.vb and click Rename

Set the name to DepartmentStore.vb, press Enter, and accept to
rename the file

To create a new class, in the Solution Explorer, right-click
DepartmentStore3 -> Add -> Class...

Notice that, this time, the default value assigned to the
member variable applies. Instead of initializing a member variable when
declaring it, you can create a method that would be used to do this. Here is an
example:

Public Class Square
Private side As Double
Public Sub SetSide()
side = 48.25
End Sub
End Class

Such a method can also be used to initialize more than one
value. When you create a method used to initialize one or more member variables
of a class, if you want the initialization to apply, you must make sure that you
call that method first before calling other methods of the class.

Just as you can create a method to initialize the
member(s)
of a class, you can overload that method with different versions to perform
different initializations. Here are examples:

When you declare a variable of a class, a special
method must be called to initialize the members of that class. This method
is automatically provided for every class and it is called a constructor.
Whenever you create a new class, a constructor is automatically provided
to it. This particular constructor is called the default
constructor. You have the option of creating it or not. Although a constructor is created for your class, you can
customize its behavior or change it as you see fit.

The constructor of a class is called New and it is
created as a sub procedure. Here is an example:

Public Class Square
Public Sub New()
End Sub
End Class

Like every method, a constructor is equipped with
a body. In this body, you can access any of the member variables (or
method(s)) of the same class. Consider the following program:

Public Module Exercise
Private Class Square
Public Sub New()
MsgBox("Square Builder")
End Sub
End Class
Public Function Main() As Integer
Dim sq As Square = New Square
Return 0
End Function
End Module

When executed, it would produce:

This shows that, when a class has been instantiated,
its constructor is the first method to be called. For this reason, you can
use a constructor to initialize a class, that is, to assign default values
to its member variables. Based on this, instead of initializing the member
variable(s) of a class when initializing it or them, or instead of
creating a special method used to initialize the member variable(s) of a
class, you can use a constructor to do this. The advantage of a
constructor is that it doesn't need to be called: it is automatically
available whenever the class is instantiated.

Practical
Learning: Constructing a Class

Create a Console Application named
DepartmentStore4

In the Solution Explorer, right-click Module1.vb and click Rename

Set the name to DepartmentStore.vb, press Enter, and accept to
rename the file

To create a new class, in the Solution Explorer, right-click
DepartmentStore4 -> Add -> Class...

Public Module DepartmentStore
Public Function Main() As Integer
Dim Item As StoreItem
Item = New StoreItem
Item.ShowItem()
Return 0
End Function
End Module

Execute the application to see the result

Close the form and return to your programming environment

A Constructor With Argument(s)

In the previous section, we saw that there was always a
default constructor for a new class that you create; you just havce the option of
explicitly creating one or not. The default constructor as we saw it doesn't
take arguments: this is not a rule, it is simply assumed. Instead of a default
constructor, you may want to create a constructor that takes an argument. Here
is an example:

Private Class Square
Public Sub New(ByVal sd As Double)
End Sub
End Class

With this type of constructor, when you declare an instance
of the class, you can use this new constructor to initialize the class. Here is
an example:

Public Module Exercise
Private Class Square
Public Sub New(ByVal sd As Double)
MsgBox("Square Builder")
End Sub
End Class
Public Function Main() As Integer
Dim sqr As Square = New Square(38.64)
Return 0
End Function
End Module

If you create one constructor for your class and pass at
least one argument to that constructor, the automatic default constructor
created by the compiler disappears. This implies that if you declare an instance
of the class and use the default constructor to initialize it, you would receive
an error when you compile the program. Based on this, the following program will
produce an error:

Public Module Exercise
Private Class Square
Public Sub New(ByVal sd As Double)
MsgBox("Square Builder")
End Sub
End Class
Public Function Main() As Integer
Dim sqr As Square = New Square ' The default constructor is not available
Return 0
End Function
End Module

If you still want to use the default constructor in a class
after creating a constructor that takes at least one argument, you must
explicitly create that default constructor.

A constructor is the primary method of a class. It allows
the programmer to initialize a variable of a class when the class is
instantiated. A constructor that plays this role of initializing an instance of
a class is also called an instance constructor. Most of the time, you don't
need to create a constructor, since one is automatically provided to any class
you create. Sometimes though, as we have seen in some classes, you need to create
your own constructor as you judge it necessary and sometimes, a single constructor
may not be sufficient. For example, when creating a class, you may decide, or
find out, that there must be more than one way for a user to initialize a
variable.

Like any other method, a constructor can be overloaded. In
other words, you can create a class and give it more than one constructor. The
same rules used on overloading regular methods also apply to constructors: the
different constructors must have different number of arguments or different
types of arguments.