Web hosting domain - Chapter 8 Object-Based Programming 321 12 // Box

Chapter 8 Object-Based Programming 321 12 // Box class definition represents a box with length, 13 // width and height dimensions 14 public class Box 15 { 16 private string[] names = { “length”, “width”, “height” }; 17 private double[] dimensions = new double[ 3 ]; 18 19 // constructor 20 public Box( double length, double width, double height ) 21 { 22 dimensions[ 0 ] = length; 23 dimensions[ 1 ] = width; 24 dimensions[ 2 ] = height; 25 } 26 27 // access dimensions by integer index number 28 public double this[ int index ] 29 { 30 get 31 { 32 return ( index < 0 || index >= dimensions.Length ) ? 33 -1 : dimensions[ index ]; 34 } 35 36 set 37 { 38 if ( index >= 0 && index < dimensions.Length ) 39 dimensions[ index ] = value; 40 } 41 42 } // end numeric indexer 43 44 // access dimensions by their string names 45 public double this[ string name ] 46 { 47 get 48 { 49 // locate element to get 50 int i =0; 51 52 while ( i < names.Length && 53 name.ToLower() != names[ i ] ) 54 i++; 55 56 return ( i == names.Length ) ? -1 : dimensions[ i ]; 57 } 58 59 set 60 { 61 // locate element to set 62 int i =0; 63 Fig. 8.16 Fig. 8.1FiFig. 8.16g. 8.16Indexers provide subscripted access to an object s members. (Part 2 of 6.) Fig. 8.16
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Leave a Reply