indexer question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • george r smith

    #1

    indexer question

    Hi all,

    I found this example in the help and thought this is great. But I can not
    figure out how to
    iterate thru the array and print out the values in the array.
    Do I use a foreach (I tried could not get that to work) or a for loop (same
    result)

    my test harness below works so I know I am getting values into the grid,
    which is a two
    dimensioned array with an indexer[char,int]
    a little code example please
    thanks
    grs

    public static void Main()
    {
    Grid aGrid = new Grid();
    aGrid['A',1] = 1;
    Console.WriteLi ne(aGrid['A',1].ToString());

    *** so here how do I loop thru the array

    =============== ========
    class Grid
    {
    const int NumRows = 26;
    const int NumCols = 10;
    int[,] cells = new int[NumRows, NumCols];
    public int this[char c, int col]
    {
    get
    {
    c = Char.ToUpper(c) ;
    if (c < 'A' || c > 'Z')
    {
    throw new ArgumentExcepti on();
    }
    if (col < 0 || col >= NumCols)
    {
    throw new IndexOutOfRange Exception();
    }
    return cells[c - 'A', col];
    }
    set
    {
    c = Char.ToUpper(c) ;
    if (c < 'A' || c > 'Z')
    {
    throw new ArgumentExcepti on();
    }
    if (col < 0 || col >= NumCols)
    {
    throw new IndexOutOfRange Exception();
    }
    cells[c - 'A', col] = value;
    }
    }
    }


  • george r smith

    #2
    Re: indexer question

    Got it, is this the standard way or is there another.
    george

    for (char aChar = 'A'; aChar < 'Z'; aChar++)
    for (int i = 0; i < 10; i++)
    Console.WriteLi ne(aGrid[aChar,i]);

    "george r smith" <gsmith@budgete xt.com> wrote in message
    news:udGd7dvrDH A.2060@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Hi all,
    >
    > I found this example in the help and thought this is great. But I can not
    > figure out how to
    > iterate thru the array and print out the values in the array.
    > Do I use a foreach (I tried could not get that to work) or a for loop[/color]
    (same[color=blue]
    > result)
    >
    > my test harness below works so I know I am getting values into the grid,
    > which is a two
    > dimensioned array with an indexer[char,int]
    > a little code example please
    > thanks
    > grs
    >
    > public static void Main()
    > {
    > Grid aGrid = new Grid();
    > aGrid['A',1] = 1;
    > Console.WriteLi ne(aGrid['A',1].ToString());
    >
    > *** so here how do I loop thru the array
    >
    > =============== ========
    > class Grid
    > {
    > const int NumRows = 26;
    > const int NumCols = 10;
    > int[,] cells = new int[NumRows, NumCols];
    > public int this[char c, int col]
    > {
    > get
    > {
    > c = Char.ToUpper(c) ;
    > if (c < 'A' || c > 'Z')
    > {
    > throw new ArgumentExcepti on();
    > }
    > if (col < 0 || col >= NumCols)
    > {
    > throw new IndexOutOfRange Exception();
    > }
    > return cells[c - 'A', col];
    > }
    > set
    > {
    > c = Char.ToUpper(c) ;
    > if (c < 'A' || c > 'Z')
    > {
    > throw new ArgumentExcepti on();
    > }
    > if (col < 0 || col >= NumCols)
    > {
    > throw new IndexOutOfRange Exception();
    > }
    > cells[c - 'A', col] = value;
    > }
    > }
    > }
    >
    >[/color]


    Comment

    • Jeffrey Tan[MSFT]

      #3
      Re: indexer question


      Hi George,

      Thanks for posting in this group.
      Foreach statement can be used to loop through arrays or collections.
      For example, in Grid class, you can expose a print method, which will use
      foreach to loop through cells array. Like this:
      public void print()
      {
      foreach(int obj in cells)
      {
      Console.WriteLi ne(obj.ToString ());
      }
      }

      Also, you can treat Grid class as a collection, but you must implement
      GetEnumerator method.
      For more information to use foreach to loop through collection , please
      refer to:
      http://msdn.microsoft.com/library/de...us/csref/html/
      vclrfusingforea chwithcollectio ns.asp

      Hope this helps,
      Best regards,
      Jeffrey Tan
      Microsoft Online Partner Support
      Get Secure! - www.microsoft.com/security
      This posting is provided "as is" with no warranties and confers no rights.

      Comment

      Working...