Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error message: Cannot convert type 'string' to 'string[]'

Tags:

c#

asp.net-3.5

I am creating a dynamic array, and getting an error:

Error message: Cannot convert type 'string' to 'string[]'

The code is:

arrTeamMembers += tb.Text;

tb.Text contains values such as "Michael | Steve | Thomas | Jeff | Susan | Helen |"

I am trying to pass the values from tb.Text to arrTeamMembers. I am NOT trying to split the text. How can I resolve this error?

like image 797
user279521 Avatar asked Feb 25 '10 21:02

user279521


3 Answers

Try this:

arrTeamMembers = tb.Text.Split('|');
like image 164
Andrew Hare Avatar answered Nov 15 '22 16:11

Andrew Hare


The problem is, arrTeamMembers is an array of strings, while tb.Text is simply a string. You need to assign tb.Text to an index in the array. To do this, use the indexer property, which looks like a number in square brackets immediately following the name of the array variable. The number in the brackets is the 0-based index in the array where you want to set the value.

arrTeamMembers[0] += tb.Text;
like image 38
David Morton Avatar answered Nov 15 '22 15:11

David Morton


You can't just add strings to an array of strings.

Depending on what you are actually trying to do, you might want this:

string[] arrTeamMembers = new string[] { tb.Text };

or

arrTeamMembers[0] = tb.Text;

You probably want to use a List instead.

List<string> stringlist = new List<string>();
stringlist.Add(tb.Text);
like image 24
Foole Avatar answered Nov 15 '22 15:11

Foole