none
Unknown error (0x80040904) while trying to run a for loop RRS feed

  • Question

  • I've been trying to build this for loop which is implementing a sorted dictionary, everything builds except the middle if statement within the for loop at the end. I can't see any reason for the fail, it seems analogous to the statements before and after although it can give multiple outputs, it shows no compilation errors. 

    My code looks like this:

                float OpeningWidth1 = 120;
                float OpeningFromSOP1 = 600;
                float OpeningWidth2 = 0;
                float OpeningFromSOP2 = 0;
                float OpeningWidth3 = 90;
                float OpeningFromSOP3 = 300;
                float OpeningWidth4 = OpeningWidth;
                float OpeningFromSOP4 = OpeningFromSOP;

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

                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<float> Dist1 = new List<float>();
                foreach (var SOPMeasure in sortedList)
                {
                    if (SOPMeasure.DistanceOverOpening > 0 && SOPMeasure.OpeningFromSOP > 0)
                    {
                        Dist1.Add(SOPMeasure.DistanceOverOpening);
                    }
                }

                List<float> Dist2 = new List<float>();
                foreach (var SOPMeasure in sortedList)
                {
                    if (SOPMeasure.DistanceOverOpening > 0 && SOPMeasure.OpeningFromSOP > 0)
                    {
                        Dist2.Add(SOPMeasure.OpeningFromSOP);
                    }
                }

                if (Dist2.Count == 0)
                {
                    GridStud single = new GridStud(Length, Height1, TimberThickness, GridCentres, OpeningHeadHeight,  OpeningCillHeight, OpeningWidth, OpeningFromSOP, 0 + GridCentres, Length);
                    float top2 = 150;
                    Canvas.SetTop(single, top2);
                    float left2 = 150;
                    Canvas.SetLeft(single, left2);
                    single.StudGrid();
                    PanelCanvas.Children.Add(single);
                }

                for (int i = 0; i <= Dist1.Count; i++)
                {
                    if (i == 0)
                    {
                        GridStud start = new GridStud(Length, Height1, TimberThickness, GridCentres, OpeningHeadHeight, OpeningCillHeight, OpeningWidth, OpeningFromSOP, 0 + GridCentres, Dist2.ElementAt(i));
                        float top3 = 150;
                        Canvas.SetTop(start, top3);
                        float left3 = 150;
                        Canvas.SetLeft(start, left3);
                        start.StudGrid();
                        PanelCanvas.Children.Add(start);
                    }

                    //if (i > 0)
                    //{
                    //    GridStud between = new GridStud(Length, Height1, TimberThickness, GridCentres, OpeningHeadHeight, OpeningCillHeight, OpeningWidth, OpeningFromSOP, Dist1.ElementAt(i - 1), Dist2.ElementAt(i) - Dist1.ElementAt(i - 1));
                    //    float top3 = 150;
                    //    Canvas.SetTop(between, top3);
                    //    float left3 = 150;
                    //    Canvas.SetLeft(between, left3);
                    //    between.StudGrid();
                    //    PanelCanvas.Children.Add(between);
                    //}

                    if (i == Dist1.Count() - 1)
                    {
                        GridStud last = new GridStud(Length, Height1, TimberThickness, GridCentres, OpeningHeadHeight, OpeningCillHeight, OpeningWidth, OpeningFromSOP, Dist1.ElementAt(i), Length - Dist1.ElementAt(i));
                        float top3 = 150;
                        Canvas.SetTop(last, top3);
                        float left3 = 150;
                        Canvas.SetLeft(last, left3);
                        last.StudGrid();
                        PanelCanvas.Children.Add(last);
                    }
                }

    I've highlighted the bit i'm struggling with in bold, any help would be welcome.

    Thanks,

    Matthew

    Wednesday, February 6, 2019 12:51 AM

Answers


  •             List<float> Dist1 = new List<float>();
                foreach (var SOPMeasure in sortedList)
                {
                    if (SOPMeasure.DistanceOverOpening > 0 && SOPMeasure.OpeningFromSOP > 0)
                    {
                        Dist1.Add(SOPMeasure.DistanceOverOpening);
                    }
                }

                List<float> Dist2 = new List<float>();
                foreach (var SOPMeasure in sortedList)
                {
                    if (SOPMeasure.DistanceOverOpening > 0 && SOPMeasure.OpeningFromSOP > 0)
                    {
                        Dist2.Add(SOPMeasure.OpeningFromSOP);
                    }
                }

                if (Dist2.Count == 0)
                {
                    GridStud single = new GridStud(Length, Height1, TimberThickness, GridCentres, OpeningHeadHeight,  OpeningCillHeight, OpeningWidth, OpeningFromSOP, 0 + GridCentres, Length);
                    float top2 = 150;
                    Canvas.SetTop(single, top2);
                    float left2 = 150;
                    Canvas.SetLeft(single, left2);
                    single.StudGrid();
                    PanelCanvas.Children.Add(single);
                }

                for (int i = 0; i <= Dist1.Count; i++)
                {
                    if (i == 0)
                    {
                        GridStud start = new GridStud(Length, Height1, TimberThickness, GridCentres, OpeningHeadHeight, OpeningCillHeight, OpeningWidth, OpeningFromSOP, 0 + GridCentres, Dist2.ElementAt(i));
                        float top3 = 150;
                        Canvas.SetTop(start, top3);
                        float left3 = 150;
                        Canvas.SetLeft(start, left3);
                        start.StudGrid();
                        PanelCanvas.Children.Add(start);
                    }

                    //if (i > 0)
                    //{
                    //    GridStud between = new GridStud(Length, Height1, TimberThickness, GridCentres, OpeningHeadHeight, OpeningCillHeight, OpeningWidth, OpeningFromSOP, Dist1.ElementAt(i - 1), Dist2.ElementAt(i) - Dist1.ElementAt(i - 1));
                    //    float top3 = 150;
                    //    Canvas.SetTop(between, top3);
                    //    float left3 = 150;
                    //    Canvas.SetLeft(between, left3);
                    //    between.StudGrid();
                    //    PanelCanvas.Children.Add(between);
                    //}


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

    Why have you gone back to using <= when you were shown in your earlier thread
    that it will cause an index out of range error? Once again, you must use

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

    as Dist1 is a List and the only valid index values are 0 to Dist1.Count - 1.

    One word of caution:

    Your for loop which determines the value of variable i uses Dist1
    but you are using i as an index into Dist2,

    Dist2.ElementAt(i));

    That is abnormal and will lead to errors when Dist1 and Dist2 have different Counts.

    Why are the lines in Bold commented out? Is the line that isn't commented
    actually a continuation of the line above it but got wrapped by the editor? 
    If it's a separate line as shown in your post then it needs to be commented 
    out as well.

    Note  that when you post code you should use the "Insert Code Block"
    button on the forum editor menu bar. That will prevent unwanted reformatting.

    - Wayne

    • Marked as answer by matty1248 Wednesday, February 6, 2019 11:25 PM
    Wednesday, February 6, 2019 2:41 AM

All replies

  • When you say it doesn't build, what error are you getting exactly? If there's a compilation error, posting its text would help.
    Wednesday, February 6, 2019 1:23 AM

  •             List<float> Dist1 = new List<float>();
                foreach (var SOPMeasure in sortedList)
                {
                    if (SOPMeasure.DistanceOverOpening > 0 && SOPMeasure.OpeningFromSOP > 0)
                    {
                        Dist1.Add(SOPMeasure.DistanceOverOpening);
                    }
                }

                List<float> Dist2 = new List<float>();
                foreach (var SOPMeasure in sortedList)
                {
                    if (SOPMeasure.DistanceOverOpening > 0 && SOPMeasure.OpeningFromSOP > 0)
                    {
                        Dist2.Add(SOPMeasure.OpeningFromSOP);
                    }
                }

                if (Dist2.Count == 0)
                {
                    GridStud single = new GridStud(Length, Height1, TimberThickness, GridCentres, OpeningHeadHeight,  OpeningCillHeight, OpeningWidth, OpeningFromSOP, 0 + GridCentres, Length);
                    float top2 = 150;
                    Canvas.SetTop(single, top2);
                    float left2 = 150;
                    Canvas.SetLeft(single, left2);
                    single.StudGrid();
                    PanelCanvas.Children.Add(single);
                }

                for (int i = 0; i <= Dist1.Count; i++)
                {
                    if (i == 0)
                    {
                        GridStud start = new GridStud(Length, Height1, TimberThickness, GridCentres, OpeningHeadHeight, OpeningCillHeight, OpeningWidth, OpeningFromSOP, 0 + GridCentres, Dist2.ElementAt(i));
                        float top3 = 150;
                        Canvas.SetTop(start, top3);
                        float left3 = 150;
                        Canvas.SetLeft(start, left3);
                        start.StudGrid();
                        PanelCanvas.Children.Add(start);
                    }

                    //if (i > 0)
                    //{
                    //    GridStud between = new GridStud(Length, Height1, TimberThickness, GridCentres, OpeningHeadHeight, OpeningCillHeight, OpeningWidth, OpeningFromSOP, Dist1.ElementAt(i - 1), Dist2.ElementAt(i) - Dist1.ElementAt(i - 1));
                    //    float top3 = 150;
                    //    Canvas.SetTop(between, top3);
                    //    float left3 = 150;
                    //    Canvas.SetLeft(between, left3);
                    //    between.StudGrid();
                    //    PanelCanvas.Children.Add(between);
                    //}


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

    Why have you gone back to using <= when you were shown in your earlier thread
    that it will cause an index out of range error? Once again, you must use

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

    as Dist1 is a List and the only valid index values are 0 to Dist1.Count - 1.

    One word of caution:

    Your for loop which determines the value of variable i uses Dist1
    but you are using i as an index into Dist2,

    Dist2.ElementAt(i));

    That is abnormal and will lead to errors when Dist1 and Dist2 have different Counts.

    Why are the lines in Bold commented out? Is the line that isn't commented
    actually a continuation of the line above it but got wrapped by the editor? 
    If it's a separate line as shown in your post then it needs to be commented 
    out as well.

    Note  that when you post code you should use the "Insert Code Block"
    button on the forum editor menu bar. That will prevent unwanted reformatting.

    - Wayne

    • Marked as answer by matty1248 Wednesday, February 6, 2019 11:25 PM
    Wednesday, February 6, 2019 2:41 AM

  • That's so great, now i just need to do the finishing touches, feeling a bit stupid about that copy paste error with the for (int i = 0; i <= Dist1.Count; i++). I totally understood that  <= issue when you pointed it out, but i must have copied it into my main file already and forgot to amend it

    I think i'm alright with the Dist1 and Dist2 counts in this instance because of the validation within,  'if (SOPMeasure.DistanceOverOpening > 0 && SOPMeasure.OpeningFromSOP > 0)', they should always be the same value , i see how it's bad practice though, i'll keep an eye on that.

    Yeh, the editor changed the formatting and wouldn't let me put in spaces to normalise it, I'll make sure to use that 'Insert Code Block tool from now on.

    Many thanks for helping out on this Wayne,

    Regards,

    Matthew


    • Edited by matty1248 Thursday, February 7, 2019 1:03 AM
    Wednesday, February 6, 2019 7:45 PM
  • Hi Ante, never saw your comment in between there. Thanks for your response, it's all running fine now. It's a small world, I work for Mitek UK (as a QA) :)
    Wednesday, February 6, 2019 11:28 PM