Home > database >  Invalid if condition
Invalid if condition

Time:11-12

How to fix it? Error: Index was out of range. Must be non-negative and less than the size of the collection.

var myAirlineNew = Airline.FromXml(fileName);
var planeList = myAirline.GetPlanes().ToList();
foreach (var plane in planeList) 
{
    for (int count = 0;  count < planeList.Count(); count  )
    {
        if (planeList[count].TakeOffWeight == planeList[count 1].TakeOffWeight)
        {
            myAirline.SortByWeight();
        }
        else myAirline.SortByNumber();

        Console.WriteLine($"Plane: {planeList[count].Type},{planeList[count].Number},{planeList[count].Fcs},{planeList[count].TakeOffWeight} kg");
    }
}

CodePudding user response:

Get rid of the inner for loop and use the plane iterator variable instead.

var myAirlineNew = Airline.FromXml(fileName);

foreach (var plane in myAirline.GetPlanes()) 
{
    if (plane.TakeOffWeight == plane.TakeOffWeight)
        myAirline.SortByWeight();
    else 
        myAirline.SortByNumber();

    Console.WriteLine($"Plane: {plane.Type},{plane.Number},{plane.Fcs},{plane.TakeOffWeight} kg");
}
  •  Tags:  
  • c#
  • Related