Pixie
Pixie

Reputation: 67

How to get the name of the element in an array

I have a data table as part of a step in spec-flow. I want to 'read' the values put into the table and record the value as well as the column/row of the table it was entered in.

I have saved each cell of the table and created a string array with all boxes, and used the following code to check if anything exists in the table:

foreach (int value in tableInfo)
{
   if (value > 0)
   {
      int number = value;           
   }
}

I have stored the number entered in the table as number, I also want to store the name of the element in the array that contains the number so I can search it against another list and assign the number to that.

Upvotes: 0

Views: 250

Answers (2)

Olivier De Meulder
Olivier De Meulder

Reputation: 2501

I think you're looking for a dictionary. A dictionary is essentially a key value pair. The key would be your cell number or cell name, and the value, the value that was in your cell. Let's say that the key is a string and the value is an int. This is how you will initialize your dictionary:

Dictationary<string, int> myDictionary = new Dictionary<string, int>();

Then as you go through that table, you add entries to the dictionary. You can use the add method, or just assign it.

myDictionary.Add("MyCellName", 20);

or

myDictionary["MyCellName"] = 20;

Later in your code, if you need to retrieve the value for cell 'xyz', you would just use:

var myValue = myDictionary["xyz"];

Upvotes: 1

rdn87
rdn87

Reputation: 724

If u have a DataTable u can use this loop:

foreach (DataRow dtRow in tableInfo.Rows)
{

    foreach(DataColumn dc in tableInfo.Columns)
    {
      var field = dtRow[dc].ToString();
    }
}

Upvotes: 0

Related Questions