Overload the FailSoftArray indexer : 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 
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.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.Indexers don't have to operate on actual arraysIndexers don't have to operate on actual arrays
5.Two dimensional indexer
6.A two-dimensional fail-soft arrayA two-dimensional fail-soft array
7.Create a specifiable range array classCreate a specifiable range array class
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.