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"?

3
  • 3
    String.Split returns a string[] not a multi-dimensional array. Commented Feb 19, 2015 at 14:24
  • 1
    This is a good solution on how to parse CSV in C# stackoverflow.com/questions/2081418/… Commented Feb 19, 2015 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. Commented Feb 19, 2015 at 16:02

1 Answer 1

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(';'));
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.