Create a specifiable range array class : Indexer « Language Basics « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Collections Data Structure
3. Components
4. Database ADO.net
5. Development Class
6. Event
7. File Stream
8. GUI Windows Form
9. Language Basics
10. Network
11. Office
12. Regular Expressions
13. Services Event
14. Thread
15. Web Services
16. Windows
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
C# / C Sharp » Language Basics » 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. Use an indexer to create a fail-soft arrayUse an indexer to create a fail-soft array
2. Overload the FailSoftArray indexerOverload the FailSoftArray indexer
3. Indexers don't have to operate on actual arraysIndexers don't have to operate on actual arrays
4. A two-dimensional fail-soft arrayA two-dimensional fail-soft array
5. Define indexerDefine indexer
6. Indexer: allow array like indexIndexer: allow array like index
7. illustrates the use of an indexer 1illustrates the use of an indexer 1
8. Implements an indexer in a classImplements an indexer in a class
9. Illustrates the use of an indexer
10. 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
11. C# Properties and Indexers
12. Indexing with an Integer IndexIndexing with an Integer Index
13. Indexing with an String Index
14. Indexing with Multiple ParametersIndexing with Multiple Parameters
www.___ja_v_a___2__s___._c__o_m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.