Create a specifiable range array class : Indexer « Class Interface « C# / C Sharp






Create a specifiable range array class

Create a specifiable range array class
/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

/* Create a specifiable range array class. 
   The RangeArray class allows indexing 
   to begin at some value other than zero. 
   When you create a RangeArray, you specify 
   the beginning and ending index. Negative 
   indexes are also  allowed.  For example, 
   you can create arrays that index from -5 to 5,  
   1 to 10, or 50 to 56. 
*/ 
  
using System;  
  
class RangeArray {   
  // private data 
  int[] a; // reference to underlying array   
  int lowerBound; // lowest index 
  int upperBound; // greatest index 
 
  // data for properties 
  int len; // underlying var for Length property 
  bool errflag; // underlying var for outcome  
    
  // Construct array given its size.  
  public RangeArray(int low, int high) {  
    high++; 
    if(high <= low) { 
      Console.WriteLine("Invalid Indices"); 
      high = 1; // create a minimal array for safety 
      low = 0; 
    } 
    a = new int[high - low];  
    len = high - low;   
 
    lowerBound = low; 
    upperBound = --high; 
  }  
  
  // Read-only Length property.  
  public int Length {  
    get {  
      return len;  
    }  
  }  
 
  // Read-only Error property.  
  public bool Error {  
    get {  
      return errflag;  
    }  
  }  
 
  // This is the indexer for RangeArray.  
  public int this[int index] {  
    // This is the get accessor.  
    get {  
      if(ok(index)) {  
        errflag = false;  
        return a[index - lowerBound];  
      } else {  
        errflag = true;  
        return 0;  
      }  
    }  
  
    // This is the set accessor  
    set {  
      if(ok(index)) {  
        a[index - lowerBound] = value;  
        errflag = false;  
      }  
      else errflag = true;  
    }  
  }  
  
  // Return true if index is within bounds.  
  private bool ok(int index) {  
    if(index >= lowerBound & index <= upperBound) return true;  
    return false;  
  }  
}   
   
// Demonstrate the index-range array.  
public class RangeArrayDemo {   
  public static void Main() {   
    RangeArray ra = new RangeArray(-5, 5);  
    RangeArray ra2 = new RangeArray(1, 10);  
    RangeArray ra3 = new RangeArray(-20, -12);  
 
    // Demonstrate ra 
    Console.WriteLine("Length of ra: " + ra.Length); 
 
    for(int i = -5; i <= 5; i++) 
      ra[i] = i; 
   
    Console.Write("Contents of ra: "); 
    for(int i = -5; i <= 5; i++) 
      Console.Write(ra[i] + " "); 
 
    Console.WriteLine("\n"); 
 
    // Demonstrate ra2 
    Console.WriteLine("Length of ra2: " + ra2.Length); 
 
    for(int i = 1; i <= 10; i++) 
      ra2[i] = i; 
 
    Console.Write("Contents of ra2: "); 
    for(int i = 1; i <= 10; i++) 
      Console.Write(ra2[i] + " "); 
 
    Console.WriteLine("\n"); 
 
    // Demonstrate ra3 
    Console.WriteLine("Length of ra3: " + ra3.Length); 
 
    for(int i = -20; i <= -12; i++) 
      ra3[i] = i; 
 
    Console.Write("Contents of ra3: "); 
    for(int i = -20; i <= -12; i++) 
      Console.Write(ra3[i] + " "); 
 
    Console.WriteLine("\n"); 
 
  }  
}

           
       








Related examples in the same category

1.indexed properties
2.Indexer with complex logic
3.Use an indexer to create a fail-soft arrayUse an indexer to create a fail-soft array
4.Overload the FailSoftArray indexerOverload the FailSoftArray indexer
5.Indexers don't have to operate on actual arraysIndexers don't have to operate on actual arrays
6.Two dimensional indexer
7.A two-dimensional fail-soft arrayA two-dimensional fail-soft array
8.Define indexerDefine indexer
9.Indexer: allow array like indexIndexer: allow array like index
10.illustrates the use of an indexer 1illustrates the use of an indexer 1
11.Implements an indexer in a classImplements an indexer in a class
12.Illustrates the use of an indexer
13.Implements an indexer and demonstrates that an indexer does not have to operate on an arrayImplements an indexer and demonstrates that an indexer does not have to operate on an array
14.C# Properties and Indexers
15.Indexing with an Integer IndexIndexing with an Integer Index
16.Indexing with an String Index
17.Indexing with Multiple ParametersIndexing with Multiple Parameters
18.Return class object from indexer