title: vb.net "For Each" iterates only once over a list
body: <p>The following &quot;for each&quot; loop runs only once. Why?</p>
  <p>The project contains 4 forms.  Form1 contains 4 PictureBox's. I'm trying to iterate over a list of forms (in this example, to set each form's BG to the picturebox) :</p>
  <pre><code>Sub Butt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Butt.Click
      Dim Forms As New List(Of Form)
      Dim Pics As New Dictionary(Of Form, PictureBox)
      Forms.Add(me)
      Forms.Add(form2)
      Forms.Add(form3)
      Forms.Add(form4)
  </code></pre>
  <p><strong>performs only first iteration:</strong></p>
  <pre><code>    For Each frm As Form In Forms
          pics(frm) = Me.Controls(&quot;PictureBox&quot; + CStr(i + 1))(0)
          'BTW the next line behave the same:
          '  pics(frm) = CType(Me.Controls(&quot;PictureBox&quot; + CStr(i + 1)), PictureBox)
          'or this:
          '  pics(frm) = PictureBox1
          'or if the pics is a dictionary(of string):
          '  pics(frm.name) = PictureBox1
      Next
  </code></pre>
  <p><strong>performs all 4 iterations :</strong></p>
  <pre><code>    For Each frm As Form In Forms
          msgbox(frm.name)
      Next
  </code></pre>
  <p>Why it wouldn't do all 4 iterations in the first loop?</p>
  <p><strong>Edit:</strong></p>
  <p>&quot;i&quot; is not the issue.
  Apparently the problem is with assigning the same control twice, as a value to the &quot;pics&quot; dictionary... still investigating.</p>
  <p><strong>Edit 2 - intermediate summary</strong></p>
  <h1>working solution (but not an answer)</h1>
  <pre><code>For x As Integer = 0 To Forms.Count - 1
      pics(Forms(x)) = DirectCast(PictureBox1, PictureBox)
  Next
  </code></pre>
  <p>Your answers can be grouped into</p>
  <h1>1. dictionary syntax: dict.add(k, v)    vs.   dict(k) = v</h1>
  <h1>2. exceotion monster eating my exception as suggested by some.</h1>
  <h1>3. casting voodoo needed, as suggested by @Andre Pageot</h1>
  <h1>4. VB don't like to add the same picturebox twice to the dictionary.</h1>
  <p>(1.) is probably not the issue. The syntax <strong>dict(k) = v</strong>  works everywhere else. I even tried dict.add(k,v) and it gave EXCATLY the same behavior, i.e. still error.</p>
  <p>(2.) There's indeed something wrong in the IDE: The debugger skips the &quot;Next&quot; statement, and continues-as-usual the next instruction after the loop. (WTF)
  It didn't matter even after I set to catch all exceptions in the Alt+Ctrl+E screen.
  The exception is still not caught.</p>
  <p>This is IMHO a serious bug, even if someone at redmond made it by design. The same &quot;funny behavior&quot; is probably inherited from the VB6 days, were sometimes code was &quot;flying away&quot; without any reason.  (But at least there it was quitting the application).</p>
  <p>p.s.  I don't have any &quot;try - catch&quot; clauses, nor &quot;on error do something stupid&quot;, just to be clear :)</p>
  <p>(3.) It's probably it, though I don't understand, and it doesn't answer the question - &quot;why the original code is doing weewooo during runtime&quot;.</p>
  <p>See &quot;working solution&quot; above.</p>
  <p>FRANKLY I DONT WANT TO UNDERSTAND IT ANYMORE BECAUSE IT DRIVES ME CRAZY, WHY, IN PYTHON, I JUST WRITE dict(k) = y  AND IT NEVER BREAKS. I DONT UNDERSTAND HOW ITS POSSIBLE TO dict.contains(k,v). IT DOESNT MAKE ANY SENSE.
  AND THERE MUST BE SENSE, IT'S PROGRAMMING, not voodoo.</p>
  <p>(4.) i don't have any way (or will) to prove it.</p>
  <p>Thank you guys.</p>
  <p>i'll probably never use VB again after this project. There are so many voodoo things to be dealt with, it doesn't worth the headaches.</p>