Multi-line text box for C # array

I am trying to pass values ​​from each row of a multiline text field into an array of strings or a multidimensional array. I also have 3 multi-line text fields that need to be placed in the same array. Below is one of the methods I tried:

ParkingTimes[0] = tbxtimeLimitS1.Text;

for (int i = 1; i <= 10; i++)
   ParkingTimes[i] = tbxparkingTimesS1.Lines;

ParkingTimes[11] = tbxtimeLimitS2.Lines;

for (int x = 0; x <= 10; x++)
   for (int i = 12; i <= 21; i++)
       ParkingTimes[i] = tbxparkingTimesS2.Lines;

ParkingTimes[11] = tbxtimeLimitS2.Lines[0];

for (int x = 0; x <= 10; x++)
    for (int i = 23; i <= 32; i++)
        ParkingTimes[i] = tbxparkingTimesS3.Lines;

What am I doing wrong? Is there a better way to do this?

+3
source share
3 answers

You can use a List instead of an array of strings. Then the AddRange method can simplify your method by eliminating the foreach loop.

List<string> ParkingTimes = new List<string>()
ParkingTimes.Add(tbxtimeLimitS1.Text);   
ParkingTimes.AddRange(tbxparkingTimesS1.Lines);
ParkingTimes.AddRange(tbxtimeLimitS2.Lines);   
ParkingTimes.AddRange(tbxparkingTimesS2.Lines);   
ParkingTimes.AddRange(tbxtimeLimitS2.Lines);   
ParkingTimes.AddRange(tbxparkingTimesS3.Lines);   

If your code still needs an array of strings, you can return the array with

string[] myLines = ParkingTimes.ToArray();

An example of this functionality List<string>can be found on MSDN here.

+4
source

string[] allLines = textbox.Text.Split('\n');

. :

foreach (string text in allLines)
{
    //do whatever with text
}
+11

- :

var totalLines = new List<String>();
totalLines.AddRange( tbxparkingTimesS1.Lines );
totalLines.AddRange( tbxparkingTimesS2.Lines );
totalLines.AddRange( tbxparkingTimesS3.Lines );

, :

var array = totalLines.ToArray();

, .

+2

All Articles