I have the following function reading CSV files from a FTP site:
public void FromCSV(string csvdata)
{
string[] data = csvdata.Split(",", StringSplitOptions.None);
StudentId = data[0];
FirstName = data[1];
LastName = data[2];
DateOfBirth = data[3];
ImageData = data[4];
}
At a certain point, I receive a empty "ImageData" and I it gives me the error bellow:
System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'
The only thing I did that brought it closer to working was:
public void FromCSV(string csvdata)
{
string[] data = csvdata.Split(",", StringSplitOptions.None);
if ((data[0] != null && data[1] != null && data[2] != null && data[3] != null && data[4] != null) && ((data[0].Length != 0 && data[1].Length != 0 && data[2].Length != 0 && data[3].Length != 0 && data[4].Length != 0)))
{
StudentId = data[0];
FirstName = data[1];
LastName = data[2];
DateOfBirth = data[3];
ImageData = data[4];
}
else
{
Console.WriteLine("CSV with incomplete data");
}
}
The problem is that it is still allowing empty strings to go to the data[0:4], hence crashing the program. What am I doing wrong? How to create a test that check for empty values in the array and how to output a message when a empty space is found?