Use an indexer to create a fail-soft array : 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.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Class Interface » Indexer 




Use an indexer to create a fail-soft array
Use an indexer to create a fail-soft array

/*
C#: The Complete Reference 
by Herbert Schildt 

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

// Use an indexer to create a fail-soft array. 
 
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 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
    
  
 
  // 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 FSDemo {  
  public static void Main() {  
    FailSoftArray fs = new FailSoftArray(5)
    int x; 
 
    // show quiet failures 
    Console.WriteLine("Fail quietly.")
    for(int i=0; i < (fs.Length * 2); i++
      fs[i= i*10
 
    for(int i=0; i < (fs.Length * 2); i++) { 
      x = fs[i]
      if(x != -1Console.Write(x + " ")
    
    Console.WriteLine()
 
    // now, generate failures 
    Console.WriteLine("\nFail with error reports.")
    for(int i=0; i < (fs.Length * 2); i++) { 
      fs[i= i*10
      if(fs.errflag
        Console.WriteLine("fs[" + i + "] out-of-bounds")
    
 
    for(int i=0; i < (fs.Length * 2); i++) { 
      x = fs[i]
      if(!fs.errflagConsole.Write(x + " ")
      else 
        Console.WriteLine("fs[" + i + "] out-of-bounds")
    
  
}


           
       














Related examples in the same category
1.indexed properties
2.Indexer with complex logic
3.Overload the FailSoftArray indexerOverload the FailSoftArray indexer
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.