I'm new to C# and having an error where I need to have the DataPoints array to return empty but I'm getting this error for whatever reason. What am I doing wrong here?
Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index"
var DataPoints = new DataPoints();
// Generate DataPoints (or something similar) from the newly constructed WspViewList.
foreach (WspViewRow row in dataPointBuilder)
{
var dataPointList = row.OriginalData.TrimStart('[').TrimEnd(']').Split(',').ToList();
DataPoints.labels.Add(dataPointList[1]);
for (var index = 2; index< dataPointList.Count; index++)
{
var dataPoint = dataPointList[index];
if (string.IsNullOrEmpty(dataPoint))
continue;
ChartDataObject cdo;
if (DataPoints.datasets.Count <= index - 2)
{
cdo = new ChartDataObject();
DataPoints.datasets.Add(cdo);
cdo.label = ColumnObjects[index].propertyName;
}
else
cdo = DataPoints.datasets[index - 2];
cdo.data.Add(dataPoint);
DataPoints.datasets[index - 2] = cdo;
}
DataPointAPI DataPointResponse = new DataPointAPI()
{
data = DataPoints,
};
dataset.Add(DataPointResponse);
}
// Set some class field to contain these datapoints
ChartData = dataset;