# Out of range exception

• ### Question

• I have my logics worked out on this for loop and everything looks ok, however the last if statement within the for loop is creating an out of range exception for some reason. I can't seem to figure out why:

using System;
using System.Collections.Generic;
using System.Linq;

namespace TestingSortingDictionary
{
public class Program : Opening
{
static void Main(string[] args)
{
int OpeningWidth1 = 80;
int OpeningFromSOP1 = 40;
int OpeningWidth2 = 80;
int OpeningFromSOP2 = 180;
int OpeningWidth3 = 60;
int OpeningFromSOP3 = 300;
int OpeningWidth4 = 80;
int OpeningFromSOP4 = 440;
int Length = 600;

List<Opening> OpeningsList = new List<Opening>(3);

OpeningsList.Add(new Opening() {OpeningFromSOP = OpeningFromSOP1, DistanceOverOpening =OpeningFromSOP1              + OpeningWidth1 });
OpeningsList.Add(new Opening() {OpeningFromSOP = OpeningFromSOP2, DistanceOverOpening =OpeningFromSOP2              + OpeningWidth2 });
OpeningsList.Add(new Opening() {OpeningFromSOP = OpeningFromSOP3, DistanceOverOpening =OpeningFromSOP3              + OpeningWidth3 });
OpeningsList.Add(new Opening() {OpeningFromSOP = OpeningFromSOP4, DistanceOverOpening =OpeningFromSOP4              + OpeningWidth4 });

var sortedList = OpeningsList.OrderBy(t => t.OpeningFromSOP);

List<int> Dist1 = new List<int>(3);
foreach (var SOPMeasure in sortedList)
{
}

List<int> Dist2 = new List<int>(3);
foreach (var SOPMeasure in sortedList)
{
}

{
for (int i = 0; i <= Dist1.Count; i++)
{
if (i == 0)
{
int ZoneSize = Dist2.ElementAt(i);
Console.WriteLine(ZoneSize);
}

else if (i > 0)
{
int ZoneSize = Dist2.ElementAt(i) - Dist1.ElementAt(i - 1);
Console.WriteLine(ZoneSize);
}

if (i == Dist1.Last())
{
int ZoneSize = Length - Dist1.ElementAt(i - 1);
Console.WriteLine(ZoneSize);
}
}
}
}
}
}

the code is also reliant on this seperate class:

namespace TestingSortingDictionary
{
public class Opening
{
private float Length { get; set; }
private float Height1 { get; set; }
private float TimberThickness { get; set; }
private float GridCentres { get; set; }
private float OpeningHeadHeight { get; set; }
private float OpeningCillHeight { get; set; }
private float OpeningWidth { get; set; }
public int OpeningFromSOP { get; set; }
public int DistanceOverOpening { get; set; }

public Opening(float length, float height1, float timberThickness, float gridCentres, float openingHeadHeight, float                openingCillHeight, float openingWidth, int openingFromSOP)
{
Length = length;
Height1 = height1;
TimberThickness = timberThickness;
GridCentres = gridCentres;
OpeningCillHeight = openingCillHeight;
OpeningWidth = openingWidth;
OpeningFromSOP = openingFromSOP;
}

public Opening()
{ }
}
}

Any help would be appreciated, thanks, Matthew

Monday, February 4, 2019 6:58 PM

• I have my logics worked out on this for loop and everything looks ok, however the last if statement within the for loop is creating an out of range exception for some reason. I can't seem to figure out why:

for (int i = 0; i <= Dist1.Count; i++)
{
if (i == 0)
{
int ZoneSize = Dist2.ElementAt(i);
Console.WriteLine(ZoneSize);
}

else if (i > 0)
{
int ZoneSize = Dist2.ElementAt(i) - Dist1.ElementAt(i - 1);
Console.WriteLine(ZoneSize);
}

if (i == Dist1.Last())
{
int ZoneSize = Length - Dist1.ElementAt(i - 1);
Console.WriteLine(ZoneSize);
}
}

What happens if you change this line:

for (int i = 0; i <= Dist1.Count; i++)

to this?

for (int i = 0; i < Dist1.Count; i++)

- Wayne

• Marked as answer by Monday, February 4, 2019 9:47 PM
Monday, February 4, 2019 8:44 PM

### All replies

• I have my logics worked out on this for loop and everything looks ok, however the last if statement within the for loop is creating an out of range exception for some reason. I can't seem to figure out why:

for (int i = 0; i <= Dist1.Count; i++)
{
if (i == 0)
{
int ZoneSize = Dist2.ElementAt(i);
Console.WriteLine(ZoneSize);
}

else if (i > 0)
{
int ZoneSize = Dist2.ElementAt(i) - Dist1.ElementAt(i - 1);
Console.WriteLine(ZoneSize);
}

if (i == Dist1.Last())
{
int ZoneSize = Length - Dist1.ElementAt(i - 1);
Console.WriteLine(ZoneSize);
}
}

What happens if you change this line:

for (int i = 0; i <= Dist1.Count; i++)

to this?

for (int i = 0; i < Dist1.Count; i++)

- Wayne

• Marked as answer by Monday, February 4, 2019 9:47 PM
Monday, February 4, 2019 8:44 PM
• Hi Wayne,

thanks for your response, I guess that does answer the question, I guess i asked the wrong question again. What i really want to know is why the last if statement isn't giving an output, I want it to give a 5th value in my sonsole of 80.

thanks,

Matthew

Monday, February 4, 2019 8:59 PM

• I guess i asked the wrong question again. What i really want to know is why the last if statement isn't giving an output, I want it to give a 5th value in my sonsole of 80.

>if (i == Dist1.Last())

Dist1.Last() returns the value of the last element in the list. Why are you
comparing the index i to that value? Maybe you meant to do this:

if (i == Dist1.Count() - 1)

>I want it to give a 5th value in my sonsole of 80.

Are you sure that's the value to expect?

- Wayne

Monday, February 4, 2019 9:36 PM
• Thanks Wayne,

that's so great. Actually I needed something more like:

if (i == Dist1.Count()-1)
{
int ZoneSize = Length - Dist1.ElementAt(i);
StudGrid();
Console.WriteLine(ZoneSize);
}

It was the syntax of the if parameter that was causing me the problems though,

Many thanks for your help,

Matthew

Monday, February 4, 2019 9:47 PM