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

I create a dynamic array and get an error message:

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

The code:

arrTeamMembers += tb.Text; 

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

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

+4
source share
4 answers

You cannot just add rows to an array of strings.

Depending on what you are actually trying to do, you may need the following:

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

or

 arrTeamMembers[0] = tb.Text; 

You might want to use a list.

 List<string> stringlist = new List<string>(); stringlist.Add(tb.Text); 
+4
source

Try the following:

 arrTeamMembers = tb.Text.Split('|'); 
+10
source

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

 arrTeamMembers[0] += tb.Text; 
+8
source

If you are trying to split text in a text field, then

 arrTeamMembers = tb.Text.Split( '|' ); 

If this does not work, are you trying to add a text field to the end of the array?

 if ( arrTeamMembers == null ) arrTeamMembers = new string[0]; string[] temp = new string[arrTeamMembers.Length + 1]; Array.Copy( temp , 0, arrTeamMembers, 0, arrTeamMembers.Length ); temp[temp.Length - 1] = tb.Text; arrTeamMembers = temp; 
+1
source

Source: https://habr.com/ru/post/1302422/


All Articles