String Array Printing in a Single Messagebox

Asked

Viewed 754 times

2

I have a variable which is an array of strings and I want to print their values, but that values and consequently the amount of them is only set after the system works.

How can I print on a MessageBox, for example, all values of all positions in one MessageBox instead of making a for and print one by one?

4 answers

3

Another option would be the following

var dataTransacao = new string[] {"01/01/2015", "02/02/2015", "03/03/2015" };
MessageBox.Show(String.Format("Datas de transação: {0}", String.Join(", ", dataTransacao)), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

3

You can use the string.Join()

In the example below the variable strDados contains all items from array comma-separated.

string[] dados = { "um", "dois", "três" };
string strDados = string.Join(", ", dados);

MessageBox.Show(strDados);

The MessageBox will show:

one, two, three

3


1

You can carry out this printing using the function Aggregate. For example:

MessageBox.Show(dataTransacao.Aggregate( (acumulador, posicao) => acumulador + ", " + posicao, "Título"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

If you declare dataTransacao thus:

var dataTransacao = new string[] {"01/01/2015", "02/02/2015", "03/03/2015" };

Will come out in the MessageBox thus:

01/01/2015, 02/02/2015, 03/03/2015

Browser other questions tagged

You are not signed in. Login or sign up in order to post.