Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Array of indexers?
Message
From
24/07/2009 16:58:06
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 3.0
OS:
Windows XP SP2
Miscellaneous
Thread ID:
01414024
Message ID:
01414380
Views:
60
>Can you post some code when you get it figured ?

It turns out it's not a big deal. You declare the indexer just like you would a single dimension array, but you access the elements as if it were a two dimensional array.

For instance (and this is off the cuff - test it at your own peril):
// create MyIndexer indexer class with override of ToString() method
public Class MyIndexer
{
  int[] myArray = new int[5];

  public int this[int element]
  {
    get {return myArray[element];}
    set {myArray[element] = value;}
  }

  // override ToString() method
  public override string ToString()
  {
    string returnString = "";

    for (int i = 0; i < 5; i++)
    {
      if (myArray[i] != 0)
      {
        returnString += myArray[i].ToString().Trim() + " ";
      }
    }
    return returnString.Trim();
  }
}
then in the program;
// set up some values and display using ToString() method
public void SetIndexerValues()
{
  MyIndexer[] ind = new MyIndexer[3];
  string Display = ""

  // Make sure indexers aren't null
  for (int i = 0; i < 3; i++)
  {
    ind[i] = new MyIndexer();
  }

  // random stuff
  ind[0][0] = 7;
  ind[0][2] = 12;
  ind[0][4] = 2;

  ind[1][2] = 15;
  ind[1][3] = 9;

  for(int i = 0; i < 5; i++)
  {
    ind[2][i] = i;
  }

  for (int i = 0; i < 3; i++)
  {
    Display += ind[i].ToString() + "\r\n";
  }
  
  MessageBox.Show(Display);
}
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform