Colm McKenna
Colm McKenna

Reputation: 5

Find length of specific dimension of 2D list

I need to check the length of the first dimension of a 2 dimensional list of integers 'centreX1' before following loop:

  for (x = 0; x < (int)centreX1[0].Count(); x++)
            {
                if (BinarySpotsInsideTolerance1[0][x] == 1)
                {
                    AllspotsY.Add(centreY1[0][x]);
                    AllspotsX.Add(centreX1[0][x]);
                    AllspotsRLU.Add(RLUSpotsthreshold1[0][x]);
                }
            }

An error is thrown at centreX1[0].Count() if centreX1 has no members.

Upvotes: 0

Views: 4705

Answers (1)

Grant Winney
Grant Winney

Reputation: 66449

You can't count the number of elements in centreX1[0], if centreX1 has no elements.

Make sure centreX1 has elements in it, before trying to access the first one.

if (centreX1.Any())  // or "if (centreX1.Count() > 0)"
{
    for (x = 0; x < (int)centreX1[0].Count(); x++)
    {
        if (BinarySpotsInsideTolerance1[0][x] == 1)
        {
            AllspotsY.Add(centreY1[0][x]);
            AllspotsX.Add(centreX1[0][x]);
            AllspotsRLU.Add(RLUSpotsthreshold1[0][x]);
        }
    }
}

Upvotes: 1

Related Questions