This is the part 2 of my refactor problem. I have successfully refactor my previous method in here:
how do i refactor these 2 methods?
The second part is similar, however I am unable to successfully refactor this 2 methods. When using a generic method, I was stuck at the line:
temp = (T)Convert.ToInt(value);
So somehow I need a different approach to refactor these 2 methods.
public static void readDataDouble(string value, ref double[][] data, ref DateTime[] timeframe, ref DateTime[] date)
{
string inputFile = "D:\\temp.csv";
string[][] temp = null;
if (File.Exists(inputFile))
{
string[] proRataVolumeFile = File.ReadAllLines(inputFile);
temp = new string[proRataVolumeFile.Length][];
for (int i = 0; i < proRataVolumeFile.Length; i++)
{
temp[i] = proRataVolumeFile[i].Split(',');
}
}
date = new DateTime[temp.Length - 1];
timeframe = new DateTime[temp[0].Length - 1];
data = new double[temp.Length - 1][];
for (int i = 1; i < temp.Length; i++)
{
data[i - 1] = new double[temp[i].Length - 1];
for (int j = 1; j < temp[i].Length; j++)
{
if (temp[i][j].Length > 0)
data[i - 1][j - 1] = Convert.ToDouble(temp[i][j]);
}
}
for (int i = 1; i < temp.Length; i++)
{
date[i - 1] = Convert.ToDateTime(temp[i][0]);
}
for (int j = 1; j < temp[0].Length; j++)
{
timeframe[j - 1] = DateTime.Parse(temp[0][j]);
}
}
public static void readDataInt(string value, ref int[][] data, ref DateTime[] timeframe, ref DateTime[] date)
{
string inputFile = "D:\\temp.csv";
string[][] temp = null;
if (File.Exists(inputFile))
{
string[] proRataVolumeFile = File.ReadAllLines(inputFile);
temp = new string[proRataVolumeFile.Length][];
for (int i = 0; i < proRataVolumeFile.Length; i++)
{
temp[i] = proRataVolumeFile[i].Split(',');
}
}
//convert the string to int
date = new DateTime[temp.Length - 1];
timeframe = new DateTime[temp[0].Length - 1];
data = new int[temp.Length - 1][];
for (int i = 1; i < temp.Length; i++)
{
data[i - 1] = new int[temp[i].Length - 1];
for (int j = 1; j < temp[i].Length; j++)
{
if (temp[i][j].Length > 0)
data[i - 1][j - 1] = Convert.ToInt32(temp[i][j]);
}
}
for (int i = 1; i < temp.Length; i++)
{
date[i - 1] = DateTime.Parse(temp[i][0]);
}
for (int j = 1; j < temp[0].Length; j++)
{
timeframe[j - 1] = DateTime.Parse(temp[0][j]);
}
}
I’d appreciate if someone post some working snippet to this problem and how can i call it.
Thanks.
You can use generics again:
Again you have to change strongly typed array to its generic counterpart, besides this the only notable change is the line where the conversion occurs. Because
stringimplementsIConvertibleyou can useIConvertible.ToType()method to convert it to any other (supported) type. Conversions to primitive types are supported, for less trivial conversions you may consider to use a conversion library like Universal Type Converter. Please note that now the conversion needs anIFormatProvider(doubles, for example, are represented in different ways for different cultures).