0

right now i try to convert data from an csv file into an multidimensional array. The data are some simple attributes for playable characters in an DOS consoleapplication (mostly text). The file is build like this

"name; gender; role; hp; selectiontoken"

There are 6 different characters (two for each role). Each character should be a row in the array and every attribute should be a column.

static void Main(string[] args)
{
    string[,] values= new string [5,6];

    var reader = new StreamReader(File.OpenRead(@"C:\path"));
    while (!reader.EndOfStream)
    {

        string line = reader.ReadLine();
        values = line.Split(';'); //Visual C# 2010 Express gives me an error at this position "an implicated conversion between string [] and string [,] is not possible"
    }

My question is, how can I split the colums in "line" to give them into "values"?

ScaleoJ
  • 3
  • 3
  • 3
    `String.Split` returns a `string[]` not a multi-dimensional array. – Tim Schmelter Feb 19 '15 at 14:24
  • 1
    This is a good solution on how to parse CSV in C# http://stackoverflow.com/questions/2081418/parsing-csv-files-in-c-sharp – ClemensOe Feb 19 '15 at 14:33
  • I can't imagine why you'd want to load these into a two-dimensional array as though you were writing Fortran 40 years ago, when you could easily create a class that has real properties and other innovations that you see in modern programming languages. – Jim Mischel Feb 19 '15 at 16:02

1 Answers1

2

Like Tim explain you, String.Split return string[]. You can store it in List<string[]> if you want.

static void Main(string[] args)
{
    List<string[]> values= new List<string[]>();

    var reader = new StreamReader(File.OpenRead(@"C:\path"));
    while (!reader.EndOfStream)
    {

        string line = reader.ReadLine();
        values.Add(line.Split(';'));
    }
}
mybirthname
  • 17,949
  • 3
  • 31
  • 55
  • The problem with this is, that i want to use the array later to display the characters and load the selected ones into variables without searching for the right row in several lists. – ScaleoJ Feb 19 '15 at 14:35
  • Better see how to read CVS to DataTable/DataSet after that you can use it for whatever you want. Also next time write your goal in the question. – mybirthname Feb 19 '15 at 14:38