Indexing with an String Index

image_pdfimage_print

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

image_pdfimage_print

   

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

image_pdfimage_print

   

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

image_pdfimage_print
   

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

  }

}

           
          


Implements an indexer in a class

image_pdfimage_print

   

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

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/

//
// Indexer.cs -- Implements an indexer in a class
//
//               Compile this program with the following command line:
//                   C:>csc Indexer.cs
//
namespace nsIndexer
{
    using System;
    
    public class Indexer
    {
        static public void Main ()
        {
            clsIndexed idx = new clsIndexed (10);
            Console.WriteLine ("The value is " + idx[3]);
            idx.Show (3);
        }
    }
    class clsIndexed
    {
        public clsIndexed (int elements)
        {
             DateTime now = DateTime.Now;
             Random rand = new Random ((int) now.Millisecond);
             Arr = new int [elements];
             for (int x = 0; x < Arr.Length; ++x)
                 Arr&#91;x&#93; = rand.Next() % 501;
        }
        int &#91;&#93; Arr;
        public int this&#91;int index&#93;
        {
            get
            {
                if ((index < 0) || (index > Arr.Length))
                    throw (new ArgumentOutOfRangeException());
                return (Arr[index]);
            }
        }
        public void Show (int index)
        {
            Console.WriteLine ("The value is " + Arr[index]);
        }
    }
}

           
          


illustrates the use of an indexer 1

image_pdfimage_print

   

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

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example10_11.cs illustrates the use of an indexer
*/

using System;


public class Example10_11a
{

  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]);

  }

}

// 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();
      }
    }
  }

}