Returning Objects


A method can return any type of data, including class types. For example, the following version of the Rect class includes a method called enlarge( ) that creates a rectangle that is proportionally the same as the invoking rectangle, but larger by a specified factor:

 // Return an object. using System; class Rect {   int width;   int height;   public Rect(int w, int h) {     width = w;     height = h;   }   public int area() {     return width * height;   }   public void show() {     Console.WriteLine(width + " " + height);   }   /* Return a rectangle that is a specified      factor larger than the invoking rectangle. */   public Rect enlarge(int factor) {     return new Rect(width * factor, height * factor);   } } class RetObj {   public static void Main() {     Rect r1 = new Rect(4, 5);     Console.Write("Dimensions of r1: ");     r1.show();     Console.WriteLine("Area of r1: " + r1.area());     Console.WriteLine();     // create a rectangle that is twice as big as r1     Rect r2 = r1.enlarge(2);     Console.Write("Dimensions of r2: ");     r2.show();     Console.WriteLine("Area of r2 " + r2.area());   } }

The output is shown here:

 Dimensions of r1: 4 5 Area of r1: 20 Dimensions of r2: 8 10 Area of r2 80

When an object is returned by a method, it remains in existence until there are no more references to it. At that point it is subject to garbage collection. Thus, an object won’t be destroyed just because the method that created it terminates.

One application of object return types is the class factory. A class factory is a method that is used to construct objects of its class. In some situations you may not want to give users of a class access to the class’ constructor because of security concerns, or because object construction depends upon certain external factors. In such cases, a class factory is used to construct objects. Here is a simple example:

 // Use a class factory. using System; class MyClass {   int a, b; // private   // Create a class factory for MyClass.   public MyClass factory(int i, int j) {     MyClass t = new MyClass();     t.a = i;     t.b = j;     return t; // return an object   }   public void show() {     Console.WriteLine("a and b: " + a + " " + b);   } } class MakeObjects {   public static void Main() {     MyClass ob = new MyClass();     int i, j;     // generate objects using the factory     for(i=0, j=10; i < 10; i++, j--) {       MyClass anotherOb = ob.factory(i, j); // make an object       anotherOb.show();     }     Console.WriteLine();   } }

The output is shown here:

 a and b: 0 10 a and b: 1 9 a and b: 2 8 a and b: 3 7 a and b: 4 6 a and b: 5 5 a and b: 6 4 a and b: 7 3 a and b: 8 2 a and b: 9 1

Let’s look closely at this example. MyClass does not define a constructor, so only the default constructor is available. Thus, it is not possible to set the values of a and b using a constructor. However, the class factory factory( ) can create objects in which a and b are given values. Moreover, since a and b are private, using factory( ) is the only way to set these values.

In Main( ), a MyClass object is instantiated, and its factory method is used inside the for loop to create ten other objects. The line of code that does this is shown here:

 MyClass anotherOb = ob.factory(i, j); // get an object

With each iteration, an object reference called anotherOb is created, and it is assigned a reference to the object constructed by the factory. At the end of each iteration of the loop, anotherOb goes out of scope, and the object to which it refers is recycled.

Returning an Array

Since in C# arrays are implemented as objects, a method can also return an array. (This differs from C++, in which arrays are not valid as return types.) For example, in the following program, the method findfactors( ) returns an array that holds the factors of the argument that it is passed:

 // Return an array. using System; class Factor {   /* Return an array containing the factors of num.      On return, numfactors will contain the number of      factors found. */   public int[] findfactors(int num, out int numfactors) {     int[] facts = new int[80]; // size of 80 is arbitrary     int i, j;     // find factors and put them in the facts array     for(i=2, j=0; i < num/2 + 1; i++)       if( (num%i)==0 ) {         facts[j] = i;         j++;       }     numfactors = j;     return facts;   } } class FindFactors {   public static void Main() {     Factor f = new Factor();     int numfactors;     int[] factors;     factors = f.findfactors(1000, out numfactors);     Console.WriteLine("Factors for 1000 are: ");     for(int i=0; i < numfactors; i++)       Console.Write(factors[i] + " ");     Console.WriteLine();   } }

The output is shown here:

 Factors for 1000 are: 2 4 5 8 10 20 25 40 50 100 125 200 250 500

In Factor, findfactors( ) is declared like this:

 public int[] findfactors(int num, out int numfactors) {

Notice how the int array return type is specified. This syntax can be generalized. Whenever a method returns an array, specify it in a similar fashion, adjusting the type and dimensions as needed. For example, the following declares a method called someMeth( ) that returns a two-dimensional array of double:

 public double[,] someMeth() { // ...




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net