0

as the title says I try to get an info from an array in foreach. This is for Microsoft form application! What I try so far is :

       private void tabPage1_Click(object sender, EventArgs e)
        {
            string[] names = new string[] { "Matt", "Joanne", "Robert" };

            foreach (string element in names)
            {
                label3.Text = (string)element[1];
            }

        }

What am I doing wrong?

1
  • 2
    Can you tell us what the underlying problem is? Even with the "fixes" I and a few others have suggested, you're still going to replace the text of that label with the last text in the array. What do you want the text of that label to be after the code has executed? Commented Aug 7, 2014 at 17:54

4 Answers 4

1

(string)element[1] gets the second character in string element and then converts that to a string. (It's equivalent to element.Substring(1, 1).)

What you probably want to do is just use element: label3.Text = element;

However, there is still an issue here, which is that label3.Text will end up being the last element of names. I doubt that this is what you want.

Sign up to request clarification or add additional context in comments.

Comments

0

element[1] is a char, as element is one of the strings from your names array.

So the error message is correct, you cannot cast a char to a string.

Perhaps you meant this:

label3.Text = element;

2 Comments

Tried with that, the problem is that I don't get any output at all. Any suggestions ?
Thanks a lot! The app was not execute the code properly
0

All you need to change is:

private void tabPage1_Click(object sender, EventArgs e)
    {
        string[] names = new string[] { "Matt", "Joanne", "Robert" };

        foreach (string element in names)
        {
            label3.Text = element;
        }

    }

This will update the label to show Matt, then Joanne, then Robert.

If you would like to do it that way, you can use a for loop

for(int i = 0; i <= 2; i++)
{
    label3.Text = names[i];
}

Comments

0

This line

label3.Text = (string)element[1];

means display the first character of each element of your string array names. Get the first character, cast it to string and assign it to label text. Explicit casting from char to string is not allowed and that is why you get error.

Cannot convert type 'char' to 'string'

You can fix that by using:

 label3.Text = element;

But this will lead to another issue, You will get end up with the last value in your array in the Label.

If you want to show all the values in an array separated by some delimiter like comma then use :

private void tabPage1_Click(object sender, EventArgs e)
{
    string[] names = new string[] { "Matt", "Joanne", "Robert" };
    label3.Text = string.Join(",", names);
}

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.