I am writing a class that contains a method that should return an array after reading from file, but it always return an empty array. When I call the method without class just in main file everything works fine.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectX
{
class ReadFile
{
private System.IO.StreamReader file;
public ReadFile()
{
file = new System.IO.StreamReader(@"E:\\XXXX.csv");
}
private int DataLenght()
{
String line = String.Empty;
int lenght = 0;
line = string.Empty;
while ((line = file.ReadLine()) != null)
{
lenght++;
}
return lenght;
}
public double[] ReadFromFile()
{
double[] arr;
String line = String.Empty;
double[] data = new double[DataLenght()];
string[] dataString = new string[DataLenght()];
List<double> listaDouble = new List<double>();
int x = 0;
String[] parts_of_line;
while ((line = file.ReadLine()) != null)
{
parts_of_line = line.Split(',');
for (int i = 0; i < parts_of_line.Length; i++)
{
parts_of_line[i] = parts_of_line[i].Trim();
data[i] = double.Parse(dataString[i]);
listaDouble.Add(data[i]);
}
}
arr = listaDouble.ToArray();
return arr;
}
}
}
and then in main I want to:
ReadFile read = new ReadFile();
double[] arr = read.ReadFromFile;
Sorry for few not needed conversion in ReadFromFile method but try few things to make it works.