A two-dimensional fail-soft array

/*
C#: The Complete Reference
by Herbert Schildt

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

// A two-dimensional fail-soft array.

using System;

class FailSoftArray2D {
int[,] a; // reference to underlying 2D array
int rows, cols; // dimensions
public int Length; // Length is public

public bool errflag; // indicates outcome of last operation

// Construct array given its dimensions.
public FailSoftArray2D(int r, int c) {
rows = r;
cols = c;
a = new int[rows, cols];
Length = rows * cols;
}

// This is the indexer for FailSoftArray2D.
public int this[int index1, int index2] {
// This is the get accessor.
get {
if(ok(index1, index2)) {
errflag = false;
return a[index1, index2];
} else {
errflag = true;
return 0;
}
}

// This is the set accessor.
set {
if(ok(index1, index2)) {
a[index1, index2] = value;
errflag = false;
}
else errflag = true;
}
}

// Return true if indexes are within bounds.
private bool ok(int index1, int index2) {
if(index1 >= 0 & index1 < rows & index2 >= 0 & index2 < cols) return true; return false; } } // Demonstrate a 2D indexer. public class TwoDIndexerDemo { public static void Main() { FailSoftArray2D fs = new FailSoftArray2D(3, 5); int x; // show quiet failures Console.WriteLine("Fail quietly."); for(int i=0; i < 6; i++) fs[i, i] = i*10; for(int i=0; i < 6; i++) { x = fs[i,i]; if(x != -1) Console.Write(x + " "); } Console.WriteLine(); // now, generate failures Console.WriteLine(" Fail with error reports."); for(int i=0; i < 6; i++) { fs[i,i] = i*10; if(fs.errflag) Console.WriteLine("fs[" + i + ", " + i + "] out-of-bounds"); } for(int i=0; i < 6; i++) { x = fs[i,i]; if(!fs.errflag) Console.Write(x + " "); else Console.WriteLine("fs[" + i + ", " + i + "] out-of-bounds"); } } } [/csharp]

Two dimensional indexer

using System;

class MultiplicationTable {
private int[,] MultiplicationArray = new int[10, 10];

public int this[int x, int y] {
get {
return MultiplicationArray[x, y];
}
set {
MultiplicationArray[x, y] = value;
}
}
public MultiplicationTable() {
for (int i = 0; i < 10; i++) { for (int y = 0; y < 10; y++) { MultiplicationArray[i, y] = i * y; } } } } class MultiplicationTableClient { public static void Main(String[] args) { MultiplicationTable MyTable = new MultiplicationTable(); Console.Write("3 x 9 is " + MyTable[3, 9]); } } [/csharp]

Indexers don't have to operate on actual arrays

/*
C#: The Complete Reference
by Herbert Schildt

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

// Indexers don't have to operate on actual arrays.

using System;

class PwrOfTwo {

/* Access a logical array that contains
the powers of 2 from 0 to 15. */
public int this[int index] {
// Compute and return power of 2.
get {
if((index >= 0) && (index < 16)) return pwr(index); else return -1; } // there is no set accessor } int pwr(int p) { int result = 1; for(int i=0; i

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 – (int) idx) < 0.5) index = (int) idx; else index = (int) idx + 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 - (int) idx) < 0.5) index = (int) idx; else index = (int) idx + 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 >= 0 & index < Length) return 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]); } } [/csharp]

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 >= 0 & index < Length) return 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 != -1) Console.Write(x + " "); } Console.WriteLine(); // now, generate failures Console.WriteLine(" Fail 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.errflag) Console.Write(x + " "); else Console.WriteLine("fs[" + i + "] out-of-bounds"); } } } [/csharp]

Indexer with complex logic

using System;

class MyValue {
private String[] Cards = new String[52];
public String this[int index] {
get {
return Cards[index];
}
set {
Cards[index] = value;
}
}

public String this[String CardName] {
get {
for (int i = 0; i < 52; i++) { if (Cards[i] == CardName) return Cards[i]; } return Cards[0]; } set { for (int i = 0; i < 52; i++) { if (Cards[i] == CardName) Cards[i] = value; } } } public MyValue() { int y = 0; int i = 0; while (i < 52) { for (int x = 0; x < 13; x++) { switch (y) { case 0: Cards[i] = (x + 1) + " A"; break; case 1: Cards[i] = (x + 1) + " B"; break; case 2: Cards[i] = (x + 1) + " C"; break; case 3: Cards[i] = (x + 1) + " D"; break; } if (y == 3) y = 0; else y++; i++; } } } } class MyValueClient { public static void Main() { MyValue PokerDeck = new MyValue(); String FourOfHearts = PokerDeck["4 of Hearts"]; Console.WriteLine(FourOfHearts); } } [/csharp]

indexed properties

   
 

using System;

public class Starter {
    public static void Main() {
        Names obj = new Names();
        Console.WriteLine(obj[1]);
    }
}

public class Names {
    object[,] _names ={{"V", 27},{"B", 35},{"D", 29}};

    public object this[int index] {
        get {
            return _names[index, 0] + " " + _names[index, 1];
        }
    }
}