Help request for working with string array foreaching

hakdmodz

New member
Joined
Sep 2, 2021
Messages
3
Programming Experience
3-5
Hi,
I'm not sure if this is the right section for posting a help request but hopefully someone can help me out.
What is the best way to do a foreach loop to get each item from a string array and fill increasing indexed item of different string array
example and my guess as to how i figure it should be processed but without success.

// list of strings to fill each indexed item in labelsList
string[] list1 = {"blah", "blah2", "blah3", "blah4", "blah5"};

//list of labels to fill from indexed items in list1 example
string[] labelsList = {Label1.Text,Label2.Text,Label3.Text,Label4.Text,Label5.Text}

foreach (string S in list1)
{
foreach(string L in labelsList)
{
labelsList[] = s;
}
}

I want outcome to be as such for this example I have a large list of labels to fill from a large list of strings read using stream reader
Label1.Text = "blah1"
Label2.Text = "blah2"
Label3.Text = "blah3"
Label4.Text = "blah4"
Label5.Text = "blah5"

can someone please help me accomplish this in minimal code
 
Solution
You wouldn't use a foreach loop in this case. A foreach loop is for when you want to do something with each item from a single list. You have two lists. The obvious option here is a for loop, so you can use the loop counter as an index into both lists.

Also, your second list doesn't make sense. You got it right in the comment you wrote above it but the list itself doesn't work. You are getting the current values of the Text properties of the Labels at that time and putting them into an array. What use is that? There's no connection to the Labels so you can't set use that list to affect that Labels later.
C#:
// list of strings to fill each indexed item in labelsList
var strings = new...
You wouldn't use a foreach loop in this case. A foreach loop is for when you want to do something with each item from a single list. You have two lists. The obvious option here is a for loop, so you can use the loop counter as an index into both lists.

Also, your second list doesn't make sense. You got it right in the comment you wrote above it but the list itself doesn't work. You are getting the current values of the Text properties of the Labels at that time and putting them into an array. What use is that? There's no connection to the Labels so you can't set use that list to affect that Labels later.
C#:
// list of strings to fill each indexed item in labelsList
var strings = new [] {"blah", "blah2", "blah3", "blah4", "blah5"};

//list of labels to fill from indexed items in list1 example
var labels = new [] {Label1, Label2, Label3, Label4, Label5};

for (var i = 0; i < strings.Length; i++)
{
    labels[i].Text = strings[i];
}
Your comment says that it's a list of Labels so actually create a list of Labels.
 
You wouldn't use a foreach loop in this case. A foreach loop is for when you want to do something with each item from a single list. You have two lists. The obvious option here is a for loop, so you can use the loop counter as an index into both lists.

Also, your second list doesn't make sense. You got it right in the comment you wrote above it but the list itself doesn't work. You are getting the current values of the Text properties of the Labels at that time and putting them into an array. What use is that? There's no connection to the Labels so you can't set use that list to affect that Labels later.
C#:
// list of strings to fill each indexed item in labelsList
var strings = new [] {"blah", "blah2", "blah3", "blah4", "blah5"};

//list of labels to fill from indexed items in list1 example
var labels = new [] {Label1, Label2, Label3, Label4, Label5};

for (var i = 0; i < strings.Length; i++)
{
    labels[i].Text = strings[i];
}
Your comment says that it's a list of Labels so actually create a list of Labels.
Thankyou for your assistance, your solution worked :)
 
Solution
Also, it doesn't sound like you should be using a StreamReader, assuming that each bit of text is its own line. The File.ReadAllLines method will return a string array containing the lines of the file in a single method call.
 
Also, it doesn't sound like you should be using a StreamReader, assuming that each bit of text is its own line. The File.ReadAllLines method will return a string array containing the lines of the file in a single method call.
Yes very true I had attempted it with stream reader but with many failured attempts went to ReadAllLines thank you for your replies.
 
Back
Top Bottom