home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / dotNETSDK / SETUP.EXE / netfxsd1.cab / FL_Interfaces_cs________.3643236F_FC70_11D3_A536_0090278A1BB8 < prev    next >
Encoding:
Text File  |  2001-08-21  |  1.1 KB  |  45 lines

  1. using System;
  2.  
  3. class Point : IComparable {
  4.    public Int32 x, y;
  5.  
  6.    public Point(Int32 x, Int32 y) {
  7.       this.x = x;
  8.       this.y = y;
  9.    }
  10.  
  11.    // CompareTo is define by the IComparable interface
  12.    public Int32 CompareTo(Object other) {
  13.       Point p = (Point) other;
  14.  
  15.       Double thisDistanceFromOrigin = Math.Sqrt(x * x + y * y);
  16.       Double otherDistanceFromOrigin = Math.Sqrt(p.x * p.x + p.y * p.y);
  17.  
  18.       return(Math.Sign(thisDistanceFromOrigin - otherDistanceFromOrigin));
  19.    }
  20.  
  21.    public override String ToString() {
  22.       return(String.Format("({0}, {1})", x, y));
  23.    }
  24. }
  25.  
  26.  
  27. class App {
  28.    static void Main() {
  29.       Point[] points = new Point[5];
  30.       points[0] = new Point(2, 2);
  31.       points[1] = new Point(3, 2);
  32.       points[2] = new Point(2, 3);
  33.       points[3] = new Point(7, 8);
  34.       points[4] = new Point(0, 1);
  35.  
  36.       Array.Sort(points);
  37.  
  38.       // Display all the elements in the array
  39.       for (Int32 i = 0; i < points.Length; i++) 
  40.          Console.WriteLine("Point {0}: {1}", i, points[i]);
  41.  
  42.       Console.Write("Press Enter to close window...");
  43.       Console.Read();
  44.    }
  45. }