-1

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.

7
  • What exactly do you mean by "item value"? If you mean the word, then w is the value. Commented Nov 16, 2022 at 23:23
  • Also, what UI framework are you using? WinForms? WebForms? WPF? ...? ASP.NET MVC doesn't seem to be related here. Commented Nov 16, 2022 at 23:25
  • I am using ASP.NET, I want this output: word1 word2 for each label text... Commented Nov 16, 2022 at 23:27
  • You mean ASP.NET WebForms? Anyway, you can achieve this using a number of ways. One would be to create an array of the labels: Label[] labels = new[] { Label1, Label2, Label3 };. Then, using a for loop, you can write something like for (int i = 0; i < wordsX.Length; i++) { labels[i].Text = wordsX[i]; }. You'd have to make sure that both arrays have the same length though, or switch to a dictionary or a list of tuples. Commented Nov 16, 2022 at 23:31
  • This is essentially a duplicate of How can i write the text of my String array into multiple Textboxes Commented Nov 16, 2022 at 23:33

3 Answers 3

0

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}); 
Sign up to request clarification or add additional context in comments.

2 Comments

the problem is that I need to pass the array items in real Label.text in ASP.NET how to do that? i am doing this in ASP.NET Webforms
It also works with real labels: Label[] labels = {Label1, Label2};
0

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];
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.