C# Impossible error...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Airslash
    New Member
    • Nov 2007
    • 221

    C# Impossible error...

    Hello,

    I have a Mdi parent form that contains a List with objects as datamember. I'm trying to get the list inside a combobox on a mdi child form.

    I'm using the following code:

    Code:
    for (int i = 0; i < ((frmMain)this.MdiParent).AssignmentStatus.Count; i++ )
                {
                    // Add the method in a save way.
                    if (true == this.InvokeRequired)
                    {
                        this.BeginInvoke(new MethodInvoker(delegate()
                        {
                            this.cmbstatus.Items.Add(((frmMain)this.MdiParent).AssignmentStatus[i]);
                        }
                        ));
                    }
                    else
                    {
                        this.cmbstatus.Items.Add(((frmMain)this.MdiParent).AssignmentStatus[i]);
                    }
                }
    Now I have 2 errors occuring which shouldn't be even happening:
    - The method enters the beginInvoke part, although the this.InvokeRequ ired is false (checked it with debug)
    - The method is executed with i = 6, while the .Count method returns 6. So according the for loop this shouldn't even be possible.
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Looks like syntax error. Is the count zero indexed?

    Comment

    • Airslash
      New Member
      • Nov 2007
      • 221

      #3
      Originally posted by kenobewan
      Looks like syntax error. Is the count zero indexed?
      Count returns the amount of elements inside the list, so 6 would mean the list counts from 0 -> 5

      Because I use i < .Count it should never reach 6, and have 5 as maximum value. The exact code works for all my other lists that I used, yet on this tiny bit it acts completly weird...

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Hmm the only thing i can think of is when the loop "ends" i=6 (that's the break condition) and since you called the function with i maybe it's using this new value of i by the time it comes around to performing the action?

        Comment

        • Airslash
          New Member
          • Nov 2007
          • 221

          #5
          Thats what I had in mind as well, but that goes against the logic of a for loop...

          Although the problem might lie in the MultiThreading environment.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Well there's no telling when in the scheme of things that invoke call will execute either.
            I am pretty sure do to the nature of what it's doing, a breakpoint will always show opposing logic

            Comment

            Working...