-1

I have static class theat declare the array:

static class GlobalDataClass
{
    public static double[,] dDataArray = new double[10, 2];   
}

Now I have a function that stream the text file line by line by having the number of rows and index of the array:

using (StreamReader sr = new StreamReader(filename))
{
    double[] dx = new double[lines]; //lines store number of rows
    double[] dy = new double[lines]; //lines store number of rows

    for (long li = 0; li < lines; li++)
    {
        dx[li] = GlobalDataClass.dDataArray[li, 0];
        dy[li] = GlobalDataClass.dDataArray[li, 1];
    }
}

My text file will be like:

1,2  
2,3  
3,4  
5,6  

Now how to have the output matrix like:

dx[1] [0,0] = 1  
dy[1] [0,1] = 2   

and so on.

Alberto
  • 13,805
  • 8
  • 42
  • 50
Ren
  • 725
  • 4
  • 13
  • 39

2 Answers2

1

For create multidimensional array, you can use list of list:

List<List<string>> ls = new List<List<string>>();
           var  filename="aa.txt";
           StreamReader sr = new StreamReader(filename);
           while (!sr.EndOfStream)
           {
               var line = sr.ReadLine();
               var element = line.Split(',');
               List<string> temp = new List<string>();
               foreach (var item in element)
               {
                   temp.Add(item);
               }
               ls.Add(temp);
           }

In this code, every line may have many element (>2).

M SH GOL
  • 199
  • 15
0

you can read the file and split each line at the comma, like this:

StreamReader sr = new StreamReader("MyNumbers.txt");
String line;
String[] lineSeperate;
line = sr.ReadLine();
lineSeperate = line.Split(',');

now, dx[index] = lineSeperate[0] and dy[index]=lineSeperate[1]

Edit You need to convert from String to double:

dx[0] = double.Parse(lineSeperate[0]);
dy[0] = double.Parse(lineSeperate[1]);
AsfK
  • 2,776
  • 3
  • 30
  • 56
  • I got the error "connot implicitly convert type 'string' to 'double'". Any solution? – Ren Feb 16 '14 at 14:37
  • @Ren, offcourse. You need to convert from string to double. see this: http://stackoverflow.com/questions/11399439/converting-string-to-double-in-c-sharp. I'll update the answer later. – AsfK Feb 17 '14 at 06:50