Consider the following code: <%@ Page Language="C#" %> <html> <head> <script runat="server"> public class Square { public double Radius; public Square() { Radius = 0.00; } public Square(double radius) { Radius = radius; } public double Perimeter() { return Radius * 4; } public double Area() { return Radius * Radius; } } public class Geometry { public void Create(Square sq) { sq.Radius = 38.62; } public string Show(Square sq) { return "<pre>Square Characteristics<br />" + "Radius: " + sq.Radius + "<br />" + "Perimeter: " + sq.Perimeter() + "<br />" + "Area: " + sq.Area() + "</pre>"; } } </script> <title>Exercise</title> </head> <body> <% Geometry figure = new Geometry(); Square sqr = new Square(); figure.Create(sqr); Response.Write(figure.Show(sqr)); %> </body> </html> This would produce: Notice that the method that receives a class as argument gives back an object that has been modified. Because a class is always used as a reference, when passing one as argument, it is implied to be passed by reference. To reinforce this, you can type the ref keyword to the left of the argument. Here is an example: <script runat="server"> public class Square { public double Radius; public Square() { Radius = 0.00; } public Square(double radius) { Radius = radius; } public double Perimeter() { return Radius * 4; } public double Area() { return Radius * Radius; } } public class Geometry { public void Create(ref Square sq) { sq.Radius = 38.62; } public string Show(Square sq) { return "<pre>Square Characteristics<br />" + "Radius: " + sq.Radius + "<br />" + "Perimeter: " + sq.Perimeter() + "<br />" + "Area: " + sq.Area() + "</pre>"; } } </script> In the same way, if passing more than one class as arguments, if you want, you can type the ref keyword to the left of each argument.
An instance of a class can be passed as an argument to one of its own methods. To do this, you primarily pass the argument as if it were any 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.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. 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. 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.
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. 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. Alternatively, you can declare an instance of the class, use the current values of the class combined with those of the instance to get new values, and then return the instance. |
|
|||||||||
|