Like a value from a regular type, you can return a class
value from a method of a class. To do this, you can first declare the method and
specify the class as the return type. Here is an example:
Public Class Point Friend x As Short Friend y As Short End Class Public Class CoordinateSystem Private PtStart As Point Private PtEnd As Point Public Function GetThePoint() As Point End Function End Class
After implementing the method, you must return a value that
is conform to the class, otherwise you would receive an error when compiling the
application. You can proceed by declaring a variable of the class in the body of
the method, initializing the variable, and then returning it. Here is an
example:
Public Class Point Friend x As Short Friend y As Short End Class Public Class CoordinateSystem Private PtStart As Point Private PtEnd As Point Public Function GetThePoint() As Point Dim Pt As Point = New Point Pt.x = CShort(InputBox("Enter the x coordinate of the point: ")) Pt.y = CShort(InputBox("Enter the y coordinate of the point: ")) Return Pt End Function End Class
Once a method has returned a value of a class, the value can
be used as normally as possible. Here is an example:
Public Module Exercise
Public Class Point
Friend x As Short
Friend y As Short
End Class
Public Class CoordinateSystem
Private PtStart As Point
Private PtEnd As Point
Public Function GetThePoint() As Point
Dim Pt As Point = New Point
Pt.x = CShort(InputBox("Enter the x coordinate of the point: "))
Pt.y = CShort(InputBox("Enter the y coordinate of the point: "))
Return Pt
End Function
End Class
Public Function Main() As Integer
Dim Coordinate As Point
Dim Coordinator As CoordinateSystem
Coordinator = New CoordinateSystem
Coordinate = Coordinator.GetThePoint()
Return 0
End Function
End Module
Once a class has been created, it can be used like any other
variable. For example, its variable can be passed as argument to a procedure or
to a method of
another class. When a class is passed as argument:
As done for the arguments of primitive
types, in the body of the procedure gets the argument, access its public or
friendly members and use them as you see fit. Here is an example:
Public Module Exercise Public Class Point Friend x As Short Friend y As Short End Class Public Class CoordinateSystem Private PtStart As Point Private PtEnd As Point Public Function GetThePoint() As Point Dim Pt As Point = New Point Pt.x = CShort(InputBox("Enter the x coordinate of the point: ")) Pt.y = CShort(InputBox("Enter the y coordinate of the point: ")) Return Pt End Function Public Sub ShowThePoint(ByVal Coord As Point) MsgBox("Point Coordinate P(" & Coord.x & ", " & Coord.y & ")") End Sub End Class Public Function Main() As Integer Dim Coordinate As Point Dim Coordinator As CoordinateSystem Coordinator = New CoordinateSystem Coordinate = Coordinator.GetThePoint() Coordinator.ShowThePoint(Coordinate) Return 0 End Function End Module
Here is an example of running the program:
As done for the arguments of primitive
types, you can pass more than one class as argument to a method.
Because classes are always used as references, when passing
a class as argument, it is implied to be passed by reference. To reinforce this,
you can type the ByRef keyword to the left of the argument. Here are
examples:
Public Module Exercise Public Class Point Friend x As Short Friend y As Short End Class Public Class CoordinateSystem Private PtStart As Point Private PtEnd As Point Public Function GetThePoint() As Point Dim Pt As Point = New Point Pt.x = CShort(InputBox("Enter the x coordinate of the point: ")) Pt.y = CShort(InputBox("Enter the y coordinate of the point: ")) Return Pt End Function Public Sub ShowTheLine(ByRef StartPoint As Point, _ ByRef EndPoint As Point) MsgBox("Line Characteristics: The line goes from P(" & _ StartPoint.x & ", " & StartPoint.y & ") to Q(" & _ EndPoint.x & ", " & EndPoint.y & ")") End Sub End Class Public Function Main() As Integer Dim First, Second As Point Dim Coordinator As CoordinateSystem Coordinator = New CoordinateSystem First = Coordinator.GetThePoint() Second = Coordinator.GetThePoint() Coordinator.ShowTheLine(First, Second) Return 0 End Function End Module
An instance of a class can be passed as an argument to one
of its own methods (if you have programmed in C++, an example of this
implementation is the copy constructor; although you can legitimately create a
copy constructor in C#, it does not have the exact same concept as in C++,
probably because C# has the Equals() method, which is actually a concept
of the .NET Framework). To do this, you primarily pass the argument as if it
were any class. Here is an example:
Public Class Point
Friend x As Integer
Friend y As Integer
Public Sub Equivalent(ByVal Same As Point)
End Sub
End Class
Then, in the body of the method, do whatever you want. You
can, or you may not, use the argument. Still, if you decide to use the argument,
know that all of the other members of the class are available through the
argument. Probably the simplest way to use the argument is the assign each of of
its values to the equivalent member of the class. Here is an example:
Public Class Point
Friend x As Integer
Friend y As Integer
Public Sub Equivalent(ByVal Same As Point)
Me.x = Same.x
Me.y = Same.y
End Sub
End Class
When calling the method, make sure you pass an instance of
the class to it. You can first create and define the class, then pass it. Here
is an example:
Public Module Exercise
Public Class Point
Friend x As Integer
Friend y As Integer
Public Sub Equivalent(ByVal Same As Point)
Me.x = Same.x
Me.y = Same.y
End Sub
End Class
Public Sub ShowPoint(ByVal Coord As Point)
MsgBox("Point Coordinate: P(" & Coord.x & ", " & Coord.y & ")")
End Sub
Public Function Main() As Integer
Dim Pt As Point = New Point
Pt.x = 4
Pt.y = 6
ShowPoint(Pt)
Dim One As Point = New Point
One.Equivalent(Pt)
ShowPoint(One)
Return 0
End Function
End Module
This would produce:
Instead of first declaring a variable of the class and
initializing it, you can create an instance of the class in the parentheses of
the calling method. To do this, you may need a constructor that can specify the
values of the fields of the class so the argument can be rightfully initialized.
Here is an example:
Public Module Exercise Public Class Point Friend x As Integer Friend y As Integer Public Sub New() End Sub Public Sub New(ByVal XCoord As Integer, ByVal YCoord As Integer) Me.x = XCoord Me.y = YCoord End Sub Public Sub Equivalent(ByVal Same As Point) Me.x = Same.x Me.y = Same.y End Sub End Class Public Sub ShowPoint(ByVal Coord As Point) MsgBox("Point Coordinate: P(" & Coord.x & ", " & Coord.y & ")") End Sub Public Function Main() As Integer Dim Pt As Point = New Point Pt.x = 4 Pt.y = 6 ShowPoint(Pt) Dim One As Point = New Point One.Equivalent(New Point(-3, 2)) ShowPoint(One) Return 0 End Function End Module
This would produce:
Instead of a formal method, you can use a constructor of the
class to pass an instance of the same class. Then, in the constructor, use the
argument as you see fit, knowing that all the members of the class are
available. Here is an example:
Public Class Point Friend x As Integer Friend y As Integer Public Sub New() End Sub Public Sub New(ByVal XCoord As Integer, ByVal YCoord As Integer) Me.x = XCoord Me.y = YCoord End Sub Public Sub New(ByVal Same As Point) Me.x = Same.x Me.y = Same.y End Sub End Class
Obviously the purpose of passing a class to one of its own
methods is not to find its equivalent. The C# language (actually the .NET
Framework) can also take care of that (through the Equals() built-in
method). Instead, you can create a method that takes an instance of the same
class but modifies that instance. For example, for our Point class, we may want
to create a new point that is distanced by one unit from the current Point
object. Here is an example of doing that:
Public Module Exercise Public Class Point Friend x As Integer Friend y As Integer Public Sub New() End Sub Public Sub New(ByVal XCoord As Integer, ByVal YCoord As Integer) Me.x = XCoord Me.y = YCoord End Sub Public Sub Equivalent(ByVal Same As Point) Me.x = Same.x Me.y = Same.y End Sub Public Sub CreatePointOneUnitAway(ByVal AddUnit As Point) Me.x = AddUnit.x + 1 Me.y = AddUnit.y + 1 End Sub End Class Public Sub ShowPoint(ByVal Coord As Point) MsgBox("Point Coordinate: P(" & Coord.x & ", " & Coord.y & ")") End Sub Public Function Main() As Integer Dim Pt As Point = New Point Pt.x = 4 Pt.y = 6 Dim One As Point = New Point One.CreatePointOneUnitAway(Pt) ShowPoint(One) One.CreatePointOneUnitAway(New Point(-8, -3)) ShowPoint(One) Return 0 End Function End Module
This would produce:
You can create a method in a class that returns an instance
of the class. To start, on the left side of the method, enter the name of the
class. Here is an example:
Public Class Point Public Function MethodName() As Point End Function End Class
There are various ways you can deal with the method. If you
want to return a new value of the class, you can declare an instance of the
class, initialize it, and then return it. Here is an example:
Public Module Exercise
Public Class Point
Friend x As Integer
Friend y As Integer
Public Sub New()
End Sub
Public Sub New(ByVal XCoord As Integer, ByVal YCoord As Integer)
Me.x = XCoord
Me.y = YCoord
End Sub
Public Sub New(ByVal Same As Point)
Me.x = Same.x
Me.x = Same.x
End Sub
Public Function AdvanceBy5() As Point
Dim Some As Point = New Point
Some.x = 5
Some.y = 5
Return Some
End Function
End Class
Public Sub ShowPoint(ByVal Coord As Point)
MsgBox("Point Coordinate: P(" & Coord.x & ", " & Coord.y & ")")
End Sub
Public Function Main() As Integer
Dim Pt As Point = New Point
Pt.x = 4
Pt.y = 6
ShowPoint(Pt)
Dim Away5 As Point = Pt.AdvanceBy5()
ShowPoint(Away5)
Return 0
End Function
End Module
This would produce:
Alternatively, you can declare an instance of the class, use
the current values of the class combined with the those of the instance to get
new values, and then return the instance.
Remember that, to call a method, if it is not static, you
will need to declare an instance of the class from where you are calling the
method. The second type of implementation consists of modifying the instance of
the class that is calling the method. For example, you can add values to its
fields or you can perform any other operation you want on the members of the
calling instance. is an example:
Public Module Exercise
Public Class Point
Friend x As Integer
Friend y As Integer
Public Sub New()
End Sub
Public Sub New(ByVal XCoord As Integer, ByVal YCoord As Integer)
Me.x = XCoord
Me.y = YCoord
End Sub
Public Sub New(ByVal Same As Point)
Me.x = Same.x
Me.x = Same.x
End Sub
REM This method adds 1 to each field of the class
REM to get a new point away North-East of the current point
Public Function CreatePointOneUnitAway() As Point
Me.x = Me.x + 1
Me.y = Me.y + 1
Return Me
End Function
End Class
Public Sub ShowPoint(ByVal Coord As Point)
MsgBox("Point Coordinate: P(" & Coord.x & ", " & Coord.y & ")")
End Sub
Public Function Main() As Integer
Dim Pt As Point = New Point
Pt.x = 4
Pt.y = 6
ShowPoint(Pt)
Dim One As Point = New Point(-8, 5)
Dim Another As Point = One.CreatePointOneUnitAway()
ShowPoint(Another)
Return 0
End Function
End Module
This would produce:
As we have learned now, you can create a method that takes
an argument that is the same type as its parent class. In the method, you can
access any member of the class, including calling the other methods of the
class.
|
Structures, Classes, and their Methods
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment