Home > OS >  How to get string array item value using foreach loop?
How to get string array item value using foreach loop?

Time:11-18

Here is the code:

string[] wordsX ={"word1", "word2","word3"}

with foreach loop want to get the item value and pass to a label

foreach (string w in wordsX)

            {

                Label1.Text = w[1].ToString();
                Label2.Text = w[2].ToString();

            }

It gives an error: Index was outside the bounds of the array.

CodePudding user response:

The problem you are facing is that you want to set the value of the label with a value of an element of wordX

Before you write some extra if statement and switches. You could use clever some linq. The trick is, create an array of those labels and you can zip them

Something like this:

public static void Main()
{
    // the words
    string[] wordsX ={"word1", "word2","word3"};
    // the labels as array
    Label[] labels = {Label1, Label2};
    
    // zip them together into a new anonymous class
    // (use the shortest collection)
    var combined = wordsX.Zip(labels, (w, l) => new { Word = w, Label = l});
    
    // display them with foreach.
    foreach (var item in combined)
    {
        // assign the label.Text to the word
        item.Label.Text = item.Word;
    }
}

Breakdown:

               // first collection
var combined = wordsX                
                   // second collection
                   .Zip(labels,      
                       // function to return new instance (class with no name)
                       (w, l) => new { Word = w, Label = l}); 

CodePudding user response:

You need to access you labels by runtime string instead of compile-time symbol. For that you can use Controls.Find.

string[] wordsX ={"word1", "word2","word3"};
for (int i=0; i< wordsX.Length; i  )
{
    var controlId = string.Format("Label{0}", i);
    var control = this.Control.Find(controlId).OfType<Label>().FirstOrDefault();
    if (control != null) control.Text = wordsX[i];
}

CodePudding user response:

Like all problems, we don't know if there are just 3 values, or say 1` to 15 values (labels on the page).

WORSE is WHY use a loop and HAVING to type in the label names - even into a array? (Defeats the WHOLE purpose of using a loop!).

So, lets assume a list, say 1 to 10 items, and thus:

 Lable1
 Lable2
 ... to 10, or 20, or 5 or 50!!!!

Ok, so not only do we need to deal with passing a list of say only 5 values, WE ALSO STILL have to blank out the ones that don't have a value (or did anyone here think of that????)

So, you can do it this way:

        List<string> Mylist = new List<string> { "one", "Two", "Three", "Four" };

        for (int i = 1; i <= 10; i  )
        {
            Label MyLabel = Page.FindControl("Label"   i) as Label;
            if (i <= Mylist.Count)
                MyLabel.Text = Mylist[i-1];
            else
                MyLabel.Text = "";
        }

Above assumes we have Label1 to label10, but it would work for 5 or 50 or whatever how many you have.

  • Related