2

I want to implement 'string[] loadedText' into 'string[] dataList', but I keep getting an error saying "Cannot implicitly convert type 'string[]' to 'string'".

string[] dataList = new string[1800];
StreamReader loadNewData = new StreamReader("podaciB.txt");
int i = 0;
while (i < 1800)
{
    string[] loadedData = loadNewData.ReadLine().Split(';');
    dataList[i] = loadedData;
    i++;
}

I need the 'dataList' array that will contain 1800 'loadedData' arrays which contain 4 strings in them.

1
  • your loadedData is an array of string and dataList as well. But when you access dataList[i] you are in a string right now and compiler waiting a string to assign it. Commented May 11, 2019 at 14:35

4 Answers 4

2

What you need is a jagged array:

string[][] dataList = new string[1800][];
Sign up to request clarification or add additional context in comments.

Comments

2

loadNewData.ReadLine().Split(';'); returns array of string and you are storing array of strings into dataList[i] i.e. string element of string array. This is the reason behind error which you mentioned in your question

If you want to store loadNewData.ReadLine().Split(';'); into an array then I would suggest you to use nested list List<List<string>>

Something like,

List<List<string>> dataList = List<List<string>>();
StreamReader loadNewData = new StreamReader("podaciB.txt");
int i = 0;
while (i < 1800)
{
    var innerList = loadNewData.ReadLine().Split(';').ToList();
    dataList.Add(innerList);
    i++;
}

Comments

1

It seems you need an array of array of strings, something like string[][].

You can do it like this:

string[][] dataList = new string[1800][];
StreamReader loadNewData = new StreamReader("podaciB.txt");
int i = 0;
while (i < 1800)
{
    string[] loadedData = loadNewData.ReadLine().Split(';');
    dataList[i] = loadedData;
    i++;
}

Comments

0

As you are splitting loadedNewData, you receive string[] already, because Split() function returns string[].

1 Comment

Can you please elaborate your answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.