MackyCute
MackyCute

Reputation: 41

How to get data out of a foreach and reuse in while and do while in c#?

In the Foreach the data has loop exactly but when the data send to while, It display single character only of each data for example: exact data is: "John" but in my code it display: J then next O, H, N until each character of the exact data is finished. Code example below:

foreach (DataGridViewRow row in DataGrid2.Rows)
            {                  
                bool isSelected = Convert.ToBoolean(row.Cells["ChkOkay"].Value);
                if (isSelected)
                {
                    value += row.Cells["EnrollName"].Value.ToString(); 
                    Console.Writeline("Selected Values " + value);
                }
            }
            while (v < value.Length())
            {                   
                    do
                    {     
        //It display single character only instead whole name.                  
                        MessageBox.Show("Name: "value[v] + "Slot: " + u);
                    foreach (var chari in value[v].ToString())
                    {
            //I re use the data here from 1st foreach
                }                        
                    v++;
                    u += 51;   
                    } while (u < 5559);

Upvotes: 0

Views: 493

Answers (2)

Caius Jard
Caius Jard

Reputation: 74605

strings can be indexed like arrays. Treating a string like an array (and adding an indexer in square brackets) returns the char at that position in the string.. "John"[0] is 'J'. Your value is a single string, not an array of strings.

With this:

value += row.Cells["EnrollName"].Value.ToString();

You're making a string value a longer and longer string. If your grid has 3 rows containing "mark", "luke" and "john", your value ends up as "marklukejohn"

If you then loop over it, you messagebox it out a char at a time

I suspect you want to retain the strings as whole strings (3 of them) in an array (or list).. in which case:

var names = new List<string>();
foreach ....

  names.Add(row.Cells["EnrollName"].Value.ToString())

Then you can later loop over the list and pull the names out as complete strings

while (v < names.Count)
{                                    
    MessageBox.Show("Name: " + names[v] + "Slot: " + u);

Your code is full of syntax errors and won't compile. Do try to get code working before you post it unless the question is about a compiler error

Upvotes: 1

persian-theme
persian-theme

Reputation: 6638

I did not understand your code, but the correct form is to take a column of DataGrid rows and place it in a list.

You then print the list elements and then check the characters of each list item.

List<string> EnrollNameList = new List<string>();
foreach (DataGridViewRow row in DataGrid2.Rows)
{
    bool isSelected = Convert.ToBoolean(row.Cells["ChkOkay"].Value);
    if (isSelected)
    {
        string colValue = row.Cells["EnrollName"].Value.ToString();
        EnrollNameList.Add(colValue);
        Console.WriteLine("Selected Values " + colValue);
    }
}
int v = 0;
while (v < EnrollNameList.Count)
{
   //message box show whole EnrollName            
   MessageBox.Show("Name: " + EnrollNameList[v]);
   foreach (var chari in EnrollNameList[v])
   {
      //get each char in EnrollName
   }
   v++;
}

Upvotes: 1

Related Questions