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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Class Interface » IndexerScreenshots 
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 <= upperBoundreturn true;  
    return false;  
  }  
}   
   
// Demonstrate the index-range array.  
public class RangeArrayDemo {   
  public static void Main() {   
    RangeArray ra = new RangeArray(-55);  
    RangeArray ra2 = new RangeArray(110);  
    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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.