C.S.
C.S.

Reputation: 379

Add string with List to a multiline textbox

I'm trying to add a list and a string in a textbox with this code

ilaninfotextbox.Text = string.Join(Environment.NewLine, ilantextinner, combinelist);

ilantextinner is a string and shows correctly on the textbox

ilantextinner is a List<string> with 20 lines in it

But ilantextinner list is showing like this in the textbox:

System.Collections.Generic.List`1[System.String]

How can I show the contents of ilantextinner in the textbox?

Note: If I use only ilantextinner contents are shown correctly.

Upvotes: 4

Views: 3504

Answers (3)

The Join function have 5 overloads. This is the one that's called using your code:

public static String Join(String separator, params object[] values);

What does it tell us?

This method receives a object array after the separator, and since it is using the params keyword, the method actually receives this on the values parameter:

new object[] { ilantextinner, combinelist };

The method will "loop" only through the objects found on the values parameter and call it's ToString().

How to solve it?

I would do something like that to reach the results you want:

combinelist.Insert(0, ilantextinner);
ilaninfotextbox.Text = string.Join(Environment.NewLine, combinelist);

The first line adds the ilantextinner to the start of the list, so you can simply use the Join overload that expects an IEnumerable<string>.

See this link for more understanding on the params keyword: https://msdn.microsoft.com/en-us/library/w5zay9db.aspx

Upvotes: 1

mohsen
mohsen

Reputation: 1806

You wanted to use

string.join(string sepeator,params string[] value);

But because you don't convert List To Array of string the compiler thinked that you would to use

string.join(string sepeator,params object[] value);

So convert List to array of string

ilaninfotextbox.Text = string.Join(Environment.NewLine, String.Join(' ', lantextinner.ToArray()), combinelist); 

Upvotes: 0

Felipe Deguchi
Felipe Deguchi

Reputation: 586

First, you need to Join the List, to make it a single string:

string joinedList = string.Join(Environment.NewLine, combinelist);

Then you join this string to the TextBox string:

ilaninfotextbox.Text = string.Join(Environment.NewLine, ilantextinner, joinedList);

Upvotes: 3

Related Questions