Indexer: allow array like index

image_pdfimage_print

/*
Learning C#
by Jesse Liberty

Publisher: O'Reilly
ISBN: 0596003765
*/
using System;

namespace Indexers
{
// a simplified ListBox control
public class ListBoxTest
{
private string[] strings;
private int ctr = 0;

// initialize the listbox with strings
public ListBoxTest(params string[] initialStrings)
{
// allocate space for the strings
strings = new String[256];

// copy the strings passed in to the constructor
foreach (string s in initialStrings)
{
strings[ctr++] = s;
}
}

// add a single string to the end of the listbox
public void Add(string theString)
{
strings[ctr] = theString;
ctr++;
}

// allow array-like access
public string this[int index]
{
get
{
if (index < 0 || index >= strings.Length)
{
// handle bad index
}
return strings[index];
}
set
{
strings[index] = value;
}
}

// helper method, given a string find
// first matching record that starts with the target
private int findString(string searchString)
{
for (int i = 0;i

Define indexer

image_pdfimage_print

/*
Learning C#
by Jesse Liberty

Publisher: O'Reilly
ISBN: 0596003765
*/
using System;

namespace Indexers
{
// a simplified ListBox control
class ListBoxTest
{
private string[] strings;
private int ctr = 0;

// initialize the listbox with strings
public ListBoxTest(params string[] initialStrings)
{
// allocate space for the strings
strings = new String[256];

// copy the strings passed in to the constructor
foreach (string s in initialStrings)
{
strings[ctr++] = s;
}
}

// add a single string to the end of the listbox
public void Add(string theString)
{
if (ctr >= strings.Length)
{
// handle bad index
}
else
strings[ctr++] = theString;
}

// allow array-like access
public string this[int index]
{
get
{
if (index < 0 || index >= strings.Length)
{
// handle bad index
}
return strings[index];
}
set
{
// add only through the add method
if (index >= ctr )
{
// handle error
}
else
strings[index] = value;
}
}

// publish how many strings you hold
public int GetNumEntries()
{
return ctr;
}

}

public class TesterListBoxTest
{
[STAThread]
static void Main()
{
// create a new listbox and initialize
ListBoxTest lbt =
new ListBoxTest(“Hello”, “World”);

Console.WriteLine(“After creation…”);
for (int i = 0;i

Create a specifiable range array class

image_pdfimage_print

/*
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 <= upperBound) return true; return false; } } // Demonstrate the index-range array. public class RangeArrayDemo { public static void Main() { RangeArray ra = new RangeArray(-5, 5); RangeArray ra2 = new RangeArray(1, 10); 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(" "); // 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(" "); // 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(" "); } } [/csharp]

A two-dimensional fail-soft array

image_pdfimage_print

/*
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

image_pdfimage_print

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

image_pdfimage_print

/*
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

image_pdfimage_print

/*
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]