-1
private static void Display(params Object[] things)
{
    foreach (Object num in number)
       Label5.Text = "{0} ", + num;
}

I'm not really sure what to really do. I want to display all the input numbers onto label5, but I'm not really sure how to do it.

For example: I have a textBox and the user enters in 23, 50, 89, 73, 40, etc into the textBox and I want to display the numbers the user entered into label5. Thank you.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Prodigy
  • 23
  • 1
  • 7

3 Answers3

2

You are overwriting the last value by every new iteration in the for loop

do instead this: put all the element in the array in one variable (use a StringBuilder) and then when that is done, set the content of the label with that variable

Example:

var sb = new StringBuilder();

foreach (Object num in number){
    sb.append(num);
}
Label5.Content = sb.ToString;

Edit

adding comma to separate the values:

var sb = new StringBuilder();
var comma = ""; 
foreach (Object num in number){
    sb.append(comma).append(num);
    comma = ", ";
}
Label5.Content = sb.ToString;
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
2

To concat IEnumerable<T> items into a string, try using String.Join:

Label5.Text = String.Join(", ", numbers);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

If you want to display the text a user has entered into a textbox, why not assign it directly

label5.Text = textbox.Text; 

The way you want to do it, to set it from an array you can do it like this:

private static void Display(params Object[] things)
{
    foreach (Object num in number)
       Label5.Text = Label5.Text + String.Format("{0}, " + num);
}

I don't quite understand though why you pass an array ob objects into the method if you don't use them.

Bojan B
  • 2,091
  • 4
  • 18
  • 26