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 Response.Write("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) Response.Write("Shape Type: " & reg.Type) Response.Write("Triangle Type: " & reg.Name) Response.Write("=-= Characteristics =-=") Response.Write("Base: " & reg.Base) Response.Write("Height: " & reg.Height) Response.Write("Area: " & reg.CalculateArea) End Sub Public Function Main() As Integer Dim Angle3 = CreateTriangle() Response.Write() ShowCharacteristics(Angle3) Response.Write() 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 Response.Write("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) Response.Write("Shape Type: " & reg.Type) Response.Write("Triangle Type: " & reg.Name) Response.Write("=-= Characteristics =-=") Response.Write("Base: " & reg.Base) Response.Write("Height: " & reg.Height) Response.Write("Area: " & reg.CalculateArea) End Sub Public Function Main() As Integer Dim Angle3 As RegularTriangle = New RegularTriangle CreateTriangle(Angle3) Response.Write() ShowCharacteristics(Angle3) Response.Write() 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.
|
Classes and Procedures
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment