Overload the FailSoftArray indexer : 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 
Overload the FailSoftArray indexer
Overload the FailSoftArray indexer

/*
C#: The Complete Reference 
by Herbert Schildt 

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

// Overload the FailSoftArray indexer. 
  
using System;  
  
class FailSoftArray {   
  int[] a;    // reference to underlying array   
  
  public int Length; // Length is public  
  
  public bool errflag; // indicates outcome of last operation  
    
  // Construct array given its size.  
  public FailSoftArray(int size) {  
    a = new int[size];  
    Length = size;   
  }  
  
  // This is the int indexer for FailSoftArray.  
  public int this[int index] {  
    // This is the get accessor.  
    get {  
      if(ok(index)) {  
        errflag = false;  
        return a[index];  
      else {  
        errflag = true;  
        return 0;  
      }  
    }  
  
    // This is the set accessor  
    set {  
      if(ok(index)) {  
        a[index= value;  
        errflag = false;  
      }  
      else errflag = true;  
    }  
  }  
  
  /* This is another indexer for FailSoftArray. 
     This index takes a double argument.  It then 
     rounds that argument to the nearest integer 
     index. */  
  public int this[double idx] {  
    // This is the get accessor.  
    get {  
      int index; 
 
      // round to nearest int 
      if( (idx - (intidx0.5index = (intidx; 
      else index = (intidx + 1
 
      if(ok(index)) {  
        errflag = false;  
        return a[index];  
      else {  
        errflag = true;  
        return 0;  
      }  
    }  
  
    // This is the set accessor  
    set {  
      int index; 
 
      // round to nearest int 
      if( (idx - (intidx0.5index = (intidx; 
      else index = (intidx + 1
 
      if(ok(index)) {  
        a[index= value;  
        errflag = false;  
      }  
      else errflag = true;  
    }  
  }  
  
  // Return true if index is within bounds.  
  private bool ok(int index) {  
   if(index >= & index < Lengthreturn true;  
   return false;  
  }  
}   
   
// Demonstrate the fail-soft array.  
public class FSDemo1 {   
  public static void Main() {   
    FailSoftArray fs = new FailSoftArray(5);  
  
    // put some values in fs 
    for(int i=0; i < fs.Length; i++
      fs[i= i;  
 
    // now index with ints and doubles 
    Console.WriteLine("fs[1]: " + fs[1])
    Console.WriteLine("fs[2]: " + fs[2])
 
    Console.WriteLine("fs[1.1]: " + fs[1.1])
    Console.WriteLine("fs[1.6]: " + fs[1.6])
 
  }  
}


           
       
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. Indexers don't have to operate on actual arraysIndexers don't have to operate on actual arrays
3. A two-dimensional fail-soft arrayA two-dimensional fail-soft array
4. Create a specifiable range array classCreate a specifiable range array class
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
ww_w_.__jav__a__2s_.___c__om | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.