Return class object from indexer

   
 
using System;

public class MyValue {
    public String Name;
}

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

    public static void Main(String[] args) {
        try {
            CardDeck PokerDeck = new CardDeck();
            MyValue HiddenAce = PokerDeck[53];
        } catch (IndexOutOfRangeException e) {
            Console.WriteLine(e.Message);
        } finally {
            // Cleanup code
        }
    }
}

    


Indexing with Multiple Parameters


   

/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/
// 19 - Indexers and EnumeratorsIndexing with Multiple Parameters
// copyright 2000 Eric Gunnerson
using System;

class Player
{
    string name;
    
    public Player(string name)
    {
        this.name = name;
    }
    
    public override string ToString()
    {
        return(name);
    }
}

class Board
{
    Player[,] board = new Player[8, 8];
    
    int RowToIndex(string row)
    {
        string temp = row.ToUpper();
        return((int) temp[0] - (int) 'A');
    }
    
    int PositionToColumn(string pos)
    {
        return(pos[1] - '0' - 1);
    }
    
    public Player this[string row, int column]
    {
        get
        {
            return(board[RowToIndex(row), column - 1]);
        }
        set
        {
            board[RowToIndex(row), column - 1] = value;
        }
    }    
    
    public Player this[string position]
    {
        get
        {
            return(board[RowToIndex(position),
            PositionToColumn(position)]);
        }
        set
        {
            board[RowToIndex(position),
            PositionToColumn(position)] = value;
        }
    }    
}

public class IndexingwithMultipleParameters
{
    public static void Main()
    {
        Board board = new Board();
        
        board["A", 4] = new Player("White King");
        board["H", 4] = new Player("Black King");
        
        Console.WriteLine("A4 = {0}", board["A", 4]);
        Console.WriteLine("H4 = {0}", board["H4"]);
    }
}

           
          


Indexing with an String Index

/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/

// 19 – Indexers and EnumeratorsIndexing with an String Index
// copyright 2000 Eric Gunnerson
using System;
using System.Collections;
class DataValue
{
public DataValue(string name, object data)
{
this.name = name;
this.data = data;
}
public string Name
{
get
{
return(name);
}
set
{
name = value;
}
}
public object Data
{
get
{
return(data);
}
set
{
data = value;
}
}
string name;
object data;
}
class DataRow
{
public DataRow()
{
row = new ArrayList();
}

public void Load()
{
/* load code here */
row.Add(new DataValue(“Id”, 5551212));
row.Add(new DataValue(“Name”, “Fred”));
row.Add(new DataValue(“Salary”, 2355.23m));
}

public DataValue this[int column]
{
get
{
return( (DataValue) row[column – 1]);
}
set
{
row[column – 1] = value;
}
}
int FindColumn(string name)
{
for (int index = 0; index < row.Count; index++) { DataValue dataValue = (DataValue) row[index]; if (dataValue.Name == name) return(index); } return(-1); } public DataValue this[string name] { get { return( (DataValue) this[FindColumn(name)]); } set { this[FindColumn(name)] = value; } } ArrayList row; } public class IndexingwithanStringIndex { public static void Main() { DataRow row = new DataRow(); row.Load(); DataValue val = row["Id"]; Console.WriteLine("Id: {0}", val.Data); Console.WriteLine("Salary: {0}", row["Salary"].Data); row["Name"].Data = "Barney"; // set the name Console.WriteLine("Name: {0}", row["Name"].Data); } } [/csharp]

Indexing with an Integer Index


   

/*
A Programmer&#039;s Introduction to C# (Second Edition)
by Eric Gunnerson

Publisher: Apress  L.P.
ISBN: 1-893115-62-3
*/

// 19 - Indexers and EnumeratorsIndexing with an Integer Index
// copyright 2000 Eric Gunnerson
using System;
using System.Collections;
class DataValue
{
    public DataValue(string name, object data)
    {
        this.name = name;
        this.data = data;
    }
    public string Name
    {
        get
        {
            return(name);
        }
        set
        {
            name = value;
        }
    }
    public object Data
    {
        get
        {
            return(data);
        }
        set
        {
            data = value;
        }
    }
    string    name;
    object data;
}
class DataRow
{
    public DataRow()
    {
        row = new ArrayList();
    }
    
    public void Load() 
    {
        /* load code here */ 
        row.Add(new DataValue("Id", 5551212));
        row.Add(new DataValue("Name", "Fred"));
        row.Add(new DataValue("Salary", 2355.23m));
    }
    
    // the indexer
    public DataValue this[int column]
    {
        get
        {
            return((DataValue) row[column - 1]);
        }
        set
        {
            row[column - 1] = value;
        }
    }
    ArrayList    row;    
}
public class IndexingwithanIntegerIndex
{
    public static void Main()
    {
        DataRow row = new DataRow();
        row.Load();
        Console.WriteLine("Column 0: {0}", row[1].Data);
        row[1].Data = 12;    // set the ID
    }
}
           
          


Implements an indexer and demonstrates that an indexer does not have to operate on an array


   

/*
C# Programming Tips &amp; Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// Seedr.cs -- Implements an indexer and demonstrates that an indexer
//             does not have to operate on an array
//
//             Compile this program with the following command line:
//                 C:>csc Seed.cs
//
namespace nsIndexer
{
    using System;
    public class Seedr
    {
        static public void Main ()
        {
            clsIndexed idx = new clsIndexed ();
            Console.WriteLine ("The value is " + idx[900]);
        }
    }
    class clsIndexed
    {
        public int this[int index]
        {
            get
            {
                DateTime now = DateTime.Now;
                Random rand = new Random ((int) now.Millisecond);
                return (rand.Next () % index);
            }
        }
    }
}


           
          


Illustrates the use of an indexer

   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example10_11.cs illustrates the use of an indexer
*/

using System;


// declare the Car class
class Car
{

  // declare two fields
  private string make;
  private string model;

  // define a constructor
  public Car(string make, string model)
  {
    this.make = make;
    this.model = model;
  }

  // define the indexer
  public string this[int index]
  {
    get
    {
      switch (index)
      {
        case 0:
          return make;
        case 1:
          return model;
        default:
          throw new IndexOutOfRangeException();
      }
    }
/*    set
      {
      switch (index)
      {
        case 0:
          this.make = value;
          break;
        case 1:
          this.model = value;
          break;
        default:
          throw new IndexOutOfRangeException();
      }
    }*/
  }

}


public class Example10_11
{

  public static void Main()
  {

    // create a Car object
    Car myCar = new Car("Toyota", "MR2");

    // display myCar[0] and myCar[1]
    Console.WriteLine("myCar[0] = " + myCar[0]);
    Console.WriteLine("myCar[1] = " + myCar[1]);

    // set myCar[0] to "Porsche" and myCar[1] to "Boxster"
    Console.WriteLine("Setting myCar[0] to "Porsche" " +
      "and myCar[1] to "Boxster"");
    myCar[0] = "Porsche";
    myCar[1] = "Boxster";
    // myCar[2] = "Test";  // causes IndeXOutOfRangeException to be thrown

    // display myCar[0] and myCar[1] again
    Console.WriteLine("myCar[0] = " + myCar[0]);
    Console.WriteLine("myCar[1] = " + myCar[1]);

  }

}