Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Invalid if condition

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");
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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");
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading