In Lesson 5, we saw that a procedure
was an assignment that complemented a program. We also saw that there were
two types of procedures: functions and sub routines. These concepts of sub
procedures and functions also apply to classes. This means that a
procedure that process a class, it can be passed a class as argument, and
it can return a class.
As described in Lesson 8, to create a sub procedure,
type the Sub
keyword followed by a name, followed by parentheses and an end of line. To
indicate the end of a sub procedure, you must type End Sub. Therefore, the
syntax of a sub procedure is:
Sub ProcedureName() End Sub
Between the Sub and the End Sub lines, you can declare the
necessary variables and they can be of regular types or based on classes. Here
is an example:
Module Exercise
Private Sub ShowCharacteristics()
Dim reg As RegularTriangle = New RegularTriangle(35.28, 26.44)
End Sub
Public Function Main() As Integer
Return 0
End Function
End Module
After declaring a variable based on a class, you can
regularly use it as we have done with objects in the Main() function so far.
Here is an example:
File: Exercise.vbModule Exercise
Private Sub ShowCharacteristics()
Dim reg As RegularTriangle = New RegularTriangle(35.28, 26.44)
Console.WriteLine("Shape Type: {0}", reg.Type)
Console.WriteLine("Triangle Type: {0}", reg.Name)
Console.WriteLine("=-= Characteristics =-=")
Console.WriteLine("Base: {0}", reg.Base)
Console.WriteLine("Height: {0}", reg.Height)
Console.WriteLine("Area: {0}", reg.CalculateArea)
End Sub
Public Function Main() As Integer
ShowCharacteristics()
Return 0
End Function
End Module
In the same way, you can declare as many class variables as
you see fit in a procedure.
So far, as we have learned since Lesson, we know that
a function can be used to return a value. In the same way, you can create
a function that returns an object of a class. When creating such a
function, set its type as that of the class it would return. The formula
to follow is still:
Function FunctionName() As ClassName End Function
In the body of the class, which is the section between
the Function and the End Function lines, you can perform any assignment
you judge necessary. For example you can declare local variables. Before
exiting the function, you must make sure it returns a value based on its As
type. You can do this using the Return keyword followed by the value to
return. Here is an example:
Private Function CreateTriangle() As RegularTriangle Dim regTri As RegularTriangle Dim base As Double, height As Double Console.WriteLine("Enter the measurements of the triangle") Console.Write("Base: ") base = CDbl(Console.ReadLine()) Console.Write("Height: ") height = CDbl(Console.ReadLine()) regTri = New RegularTriangle(base, height) Return regTri End Function
After defining the function, since it returns a value,
when calling it, you can assign it to a variable of the type that it
returns. Here is an example:
Module Exercise
Private Function CreateTriangle() As RegularTriangle
Dim regTri As RegularTriangle
Dim base As Double, height As Double
Console.WriteLine("Enter the measurements of the triangle")
Console.Write("Base: ")
base = CDbl(Console.ReadLine())
Console.Write("Height: ")
height = CDbl(Console.ReadLine())
regTri = New RegularTriangle(base, height)
Return regTri
End Function
Private Sub ShowCharacteristics()
Dim reg As RegularTriangle = New RegularTriangle
reg = CreateTriangle()
Console.WriteLine("Shape Type: {0}", reg.Type)
Console.WriteLine("Triangle Type: {0}", reg.Name)
Console.WriteLine("=-= Characteristics =-=")
Console.WriteLine("Base: {0}", reg.Base)
Console.WriteLine("Height: {0}", reg.Height)
Console.WriteLine("Area: {0}", reg.CalculateArea)
End Sub
Public Function Main() As Integer
ShowCharacteristics
Return 0
End Function
End Module
Here is a test of the above code:
Enter the measurements of the triangle Base: 18.44 Height: 12.62 Shape Type: Triangle Triangle Type: Regular =-= Characteristics =-= Base: 18.44 Height: 12.62 Area: 116.3564
Like a regular variable, a class can be passed as
argument to a procedure. When a procedure receives such an argument, it
can process it as necessary. All the rules we reviewed for regular
variables apply to a class, as long as you keep in mind that an object has
members that you may need to be aware of. When calling the procedure, make
sure you pass it a value argument based on the class it passed to it. Here
is an example of a class passed as argument:
Module Exercise Private Function CreateTriangle() As RegularTriangle Dim regTri As RegularTriangle Dim base As Double, height As Double Console.WriteLine("Enter the measurements of the triangle") Console.Write("Base: ") base = CDbl(Console.ReadLine()) Console.Write("Height: ") height = CDbl(Console.ReadLine()) regTri = New RegularTriangle(base, height) Return regTri End Function Private Sub ShowCharacteristics(ByVal reg As RegularTriangle) Console.WriteLine("Shape Type: {0}", reg.Type) Console.WriteLine("Triangle Type: {0}", reg.Name) Console.WriteLine("=-= Characteristics =-=") Console.WriteLine("Base: {0}", reg.Base) Console.WriteLine("Height: {0}", reg.Height) Console.WriteLine("Area: {0}", reg.CalculateArea) End Sub Public Function Main() As Integer Dim Angle3 = CreateTriangle() Console.WriteLine() ShowCharacteristics(Angle3) Console.WriteLine() Return 0 End Function End Module
Besides the function, in Lesson
5, we saw that, by passing an argument by reference, a sub procedure
could return a value. This characteristic also applies to a class passed
as argument. When passing the argument, precede it with the ByRef
keyword. In the procedure, process the argument as you see fit, unless you
choose not to. When calling the procedure, pass it a valid variable based
on the type of the argument. Here is an example:
Module Exercise Private Sub CreateTriangle(ByRef regTri As RegularTriangle) Dim base As Double, height As Double Console.WriteLine("Enter the measurements of the triangle") Console.Write("Base: ") base = CDbl(Console.ReadLine()) Console.Write("Height: ") height = CDbl(Console.ReadLine()) regTri = New RegularTriangle(base, height) End Sub Private Sub ShowCharacteristics(ByVal reg As RegularTriangle) Console.WriteLine("Shape Type: {0}", reg.Type) Console.WriteLine("Triangle Type: {0}", reg.Name) Console.WriteLine("=-= Characteristics =-=") Console.WriteLine("Base: {0}", reg.Base) Console.WriteLine("Height: {0}", reg.Height) Console.WriteLine("Area: {0}", reg.CalculateArea) End Sub Public Function Main() As Integer Dim Angle3 As RegularTriangle = New RegularTriangle CreateTriangle(Angle3) Console.WriteLine() ShowCharacteristics(Angle3) Console.WriteLine() Return 0 End Function End Module
Here is an example of testing it:
Enter the measurements of the triangle Base: 50.05 Height: 25.52 Shape Type: Triangle Triangle Type: Regular =-= Characteristics =-= Base: 50.05 Height: 25.52 Area: 638.638
Like a regular variable, a class can be locally
declared as static, using the Static keyword. If the value of
such a locally declared variable changes, when the procedure ends, the
value of the variable is changed and would be kept until the next call.
When passing an argument of a class, you can specify
that it is not required. Such an argument is considered optional. To specify that an argument is optional, when creating
its procedure, type the Optional keyword to the left of the
argument's name and assign it the default value. All the other rules we
reviewed for optional arguments are also applied here.
If you want to create various procedures that takes a
class argument, you can create a procedure with the same name but
different signatures. This is referred to as overloading the procedure.
When doing this, follow the same rules we reviewed for overloading a
procedure: the must have the same name, they cannot have the same number
of argument when the arguments at each position are of the same types.
All of the classes we have used so far can serve as
parents of other classes. This is the default behavior of a regular class:
the ability to derive a new class from it. In some cases, you may not want
any class to be derived from a particular class you are creating. Such a
class is referred to as sealed.
|
Class Abstraction
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment