Problem in FOR loop ... Is it a BUG ?!!<br><br><br>Hello all ,<br><br>I discovered that error by chance when I was working on Collections. But I found it in FOR loop in VB.Net.<br><br>Have a Look :<br><div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:Courier New;font-size:11px"><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap"><tbody><tr><td><font style="font-size:11px"></font><font style="color:blue">Sub</font><font style="font-size:11px"> Main()  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">       </td></tr><tr><td>        <font style="color:blue">Dim</font><font style="font-size:11px"> Y </font><font style="color:blue">As</font><font style="font-size:11px"> </font><font style="color:blue">Integer</font><font style="font-size:11px"> = 10  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">For</font><font style="font-size:11px"> x </font><font style="color:blue">As</font><font style="font-size:11px"> </font><font style="color:blue">Integer</font><font style="font-size:11px"> = 0 </font><font style="color:blue">To</font><font style="font-size:11px"> Y  </font></td></tr><tr><td>            Console.WriteLine(x.ToString())  </td></tr><tr><td style="background-color:rgb(247, 247, 247)">            Y = Y - 1  </td></tr><tr><td>        <font style="color:blue">Next</font><font style="font-size:11px">  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">        Console.ReadLine()  </td></tr><tr><td><font style="color:blue">End</font><font style="font-size:11px"> </font><font style="color:blue">Sub</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)"> </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)"><font style="color:green">'and the result was</font><font style="font-size:11px"> </font></td></tr><tr><td>0 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">1 </td></tr><tr><td>2 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">3 </td></tr><tr><td>4 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">5 </td></tr><tr><td>6 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">7 </td></tr><tr><td>8 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">9 </td></tr><tr><td>10 </td></tr><tr><td style="background-color:rgb(247, 247, 247)"> </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)"><font style="color:green">'and that is not logically correct !!</font><font style="font-size:11px"> </font></td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)"><font style="color:green">'the Correct result should be :</font><font style="font-size:11px"> </font></td></tr><tr><td>0 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">1 </td></tr><tr><td>2 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">3 </td></tr><tr><td>4 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">5 </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)"><font style="color:green">'as Y decreased every step.</font><font style="font-size:11px"> </font></td></tr></tbody></table></div><br>In other words : why FOR loop limit is not updated every step!!!! ? Is it a Bug or What ?!<br><br><br>thanks in advance !!<br><br><br><br><br><br>© 2009 Microsoft Corporation. All rights reserved.Wed, 05 Nov 2008 16:31:34 Ze7b47ed3-834e-4e05-8357-00ca92fc8a1fhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#e7b47ed3-834e-4e05-8357-00ca92fc8a1fhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#e7b47ed3-834e-4e05-8357-00ca92fc8a1fThe_knight5000http://social.msdn.microsoft.com/Profile/en-US/?user=The_knight5000Problem in FOR loop ... Is it a BUG ?!!<br><br><br>Hello all ,<br><br>I discovered that error by chance when I was working on Collections. But I found it in FOR loop in VB.Net.<br><br>Have a Look :<br><div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:Courier New;font-size:11px"><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap"><tbody><tr><td><font style="font-size:11px"></font><font style="color:blue">Sub</font><font style="font-size:11px"> Main()  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">       </td></tr><tr><td>        <font style="color:blue">Dim</font><font style="font-size:11px"> Y </font><font style="color:blue">As</font><font style="font-size:11px"> </font><font style="color:blue">Integer</font><font style="font-size:11px"> = 10  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">For</font><font style="font-size:11px"> x </font><font style="color:blue">As</font><font style="font-size:11px"> </font><font style="color:blue">Integer</font><font style="font-size:11px"> = 0 </font><font style="color:blue">To</font><font style="font-size:11px"> Y  </font></td></tr><tr><td>            Console.WriteLine(x.ToString())  </td></tr><tr><td style="background-color:rgb(247, 247, 247)">            Y = Y - 1  </td></tr><tr><td>        <font style="color:blue">Next</font><font style="font-size:11px">  </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">        Console.ReadLine()  </td></tr><tr><td><font style="color:blue">End</font><font style="font-size:11px"> </font><font style="color:blue">Sub</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)"> </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)"><font style="color:green">'and the result was</font><font style="font-size:11px"> </font></td></tr><tr><td>0 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">1 </td></tr><tr><td>2 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">3 </td></tr><tr><td>4 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">5 </td></tr><tr><td>6 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">7 </td></tr><tr><td>8 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">9 </td></tr><tr><td>10 </td></tr><tr><td style="background-color:rgb(247, 247, 247)"> </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)"><font style="color:green">'and that is not logically correct !!</font><font style="font-size:11px"> </font></td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)"><font style="color:green">'the Correct result should be :</font><font style="font-size:11px"> </font></td></tr><tr><td>0 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">1 </td></tr><tr><td>2 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">3 </td></tr><tr><td>4 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">5 </td></tr><tr><td> </td></tr><tr><td style="background-color:rgb(247, 247, 247)"><font style="color:green">'as Y decreased every step.</font><font style="font-size:11px"> </font></td></tr></tbody></table></div><br>In other words : why FOR loop limit is not updated every step!!!! ? Is it a Bug or What ?!<br><br><br>thanks in advance !!<br><br><br><br><br><br>Sun, 02 Nov 2008 23:58:42 Z2008-11-02T23:58:42Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#91c23f97-559f-422a-b25e-80e4de8fcde3http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#91c23f97-559f-422a-b25e-80e4de8fcde3JohnWeinhttp://social.msdn.microsoft.com/Profile/en-US/?user=JohnWeinProblem in FOR loop ... Is it a BUG ?!! By design.  Try increasing the lower limit within the loop.Mon, 03 Nov 2008 00:02:51 Z2008-11-03T00:02:51Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#e70dd745-e1c3-487b-a8a3-fb6d79d59af0http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#e70dd745-e1c3-487b-a8a3-fb6d79d59af0David Antonhttp://social.msdn.microsoft.com/Profile/en-US/?user=David%20AntonProblem in FOR loop ... Is it a BUG ?!!The ending condition of VB for loops is always evaluated just once before the looping begins.  This is different from the 'C-style' languages, such as C#, C++, and Java, where the ending condition is constantly re-evaluated.<br><br><hr size="1" align="left" width="25%">Convert between VB, C#, C++, and Java (http://www.tangiblesoftwaresolutions.com)Mon, 03 Nov 2008 00:16:47 Z2008-11-03T00:16:47Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#db255991-b9a9-495d-81e0-735bde7486cchttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#db255991-b9a9-495d-81e0-735bde7486ccThe_knight5000http://social.msdn.microsoft.com/Profile/en-US/?user=The_knight5000Problem in FOR loop ... Is it a BUG ?!! Hello ,<br><br>@<span class=postedBy><a title=JohnWein href="http://social.msdn.microsoft.com/en-US/profile/?user=JohnWein&amp;referrer=http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f/"><font color="#0072bc">JohnWein</font></a>: I didn't geet it , what do u mean !?<br><br>@<span class=postedBy><a title="David Anton" href="http://social.msdn.microsoft.com/en-US/profile/?user=David Anton&amp;referrer=http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f/"><font color="#0072bc">David Anton</font></a></span> : I got it recently , and i think it is not wise logic , Is it ?! why did they make it like that ?!<br></span>Mon, 03 Nov 2008 00:25:58 Z2008-11-03T00:25:58Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#fb2ce3f9-abc5-4f6f-96e3-056111cdf6fbhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#fb2ce3f9-abc5-4f6f-96e3-056111cdf6fbDavid Antonhttp://social.msdn.microsoft.com/Profile/en-US/?user=David%20AntonProblem in FOR loop ... Is it a BUG ?!! I'm not sure, but it's consistent.  The VB 'For' loop is very static - there is nothing executed in the actual 'For' statement after the initial evaluation.  In C#/C++/Java, the initializer is evaluated just once, but the other two statements (usually used for the ending condition and loop incrementer, although you can put any statements there) are executed on every iteration. <hr size="1" align="left" width="25%">Convert between VB, C#, C++, and Java (http://www.tangiblesoftwaresolutions.com)Mon, 03 Nov 2008 00:37:48 Z2008-11-03T00:37:48Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#29102ca3-6fa2-4f9c-8b15-ca23491e8f07http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#29102ca3-6fa2-4f9c-8b15-ca23491e8f07The_knight5000http://social.msdn.microsoft.com/Profile/en-US/?user=The_knight5000Problem in FOR loop ... Is it a BUG ?!! mmm , I'm not talking about &quot;the actual 'For' statement&quot; ,it was in the code block !! and actually it raises OutOfRange exceptions when u use it like that with collections , or some logic errors just like above snippet !!<br><hr size="1" align="left" width="25%">MCP (Developing Web Applications ) MCP (Developing Windows Applications ) MCP (Designing and Implementing Databases with Microsoft® SQL Server™ 2000)Mon, 03 Nov 2008 01:26:39 Z2008-11-03T01:26:39Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#e4c568a8-6c7b-486a-ae46-af89da8bfdf6http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#e4c568a8-6c7b-486a-ae46-af89da8bfdf6John Anthony Oliverhttp://social.msdn.microsoft.com/Profile/en-US/?user=John%20Anthony%20OliverProblem in FOR loop ... Is it a BUG ?!! Hi The_knight5000,<br><br>As is often said in these forums, <strong><u>VB.Net is NOT C</u></strong> is <u><strong>NOT</strong></u> C# and is <strong><u>NOT</u></strong> C++<br><br><br><br><strong><u>Expect different things to happen in VB.Net<br></u></strong><br><br><br>However you do raise an interesting point.<br><br>You can only affect the START and END values of a FOR NEXT loop in VB.Net<br> <strong><u>from outside the loop</u></strong> it seems as I have just tried it.<br><br><font size=2> <p></font><font color="#0000ff" size=2><br><font color="#0000ff" size=2></p> <p>Dim</font><font size=2> sb </font><font color="#0000ff" size=2>As</font> <font color="#0000ff" size=2>New</font><font size=2> System.Text.StringBuilder</p> <p></font>Dim</font><font size=2> <font style="background-color:#000000" color="#ffff00">LoopStart</font> </font><font color="#0000ff" size=2>As</font> <font color="#0000ff" size=2>Integer</font><font size=2> = 0<br></font><font color="#0000ff" size=2><br>Dim</font><font size=2> <font style="background-color:#000000" color="#ffcc33">LoopEnd</font> </font><font color="#0000ff" size=2>As</font> <font color="#0000ff" size=2>Integer</font><font size=2> = 10<br></font><font color="#0000ff" size=2><br>For</font><font size=2> x </font><font color="#0000ff" size=2>As</font> <font color="#0000ff" size=2>Integer</font><font size=2> = <font style="background-color:#000000" color="#ffff00">LoopStart</font> </font><font color="#0000ff" size=2>To</font><font size=2> <font style="background-color:#000000" color="#ffcc33">LoopEnd</font><br><br></font><font style="font-size:16px;background-color:#000000" color="#33ff33"><strong>'You <font color="#ff0000">can NOT</font> change <font color="#ffff00">LoopStart</font> or <font style="background-color:#000000" color="#ffcc33">LoopEnd</font> within<br></strong></font><font color="#008000" size=2><strong><font style="font-size:16px;background-color:#000000" color="#33ff33">' a <font color="#ccffff">FOR NEXT</font> loop within Vb.Net as I have just tried it.</font></strong><br></font><font size=2>sb.Append(x.ToString &amp; </font><font color="#a31515" size=2>&quot; &quot;</font><font size=2>)<br></font><font color="#0000ff" size=2>Next</font><font size=2> x<br><br><font size=2>MessageBox.Show(sb.ToString)<br><br><br><br><br>Then try this.&gt;&gt;<br><br><font color="#0000ff" size=2></p> <p>Option</font> <font color="#0000ff" size=2>Strict</font> <font color="#0000ff" size=2>On<br>Public</font> <font color="#0000ff" size=2>Class</font><font size=2> Form1<br><br></font><font color="#0000ff" size=2>Private</font> <font color="#0000ff" size=2>Sub</font><font size=2> Button1_Click(</font><font color="#0000ff" size=2>ByVal</font><font size=2> sender </font><font color="#0000ff" size=2>As</font><font size=2> System.Object, </font><font color="#0000ff" size=2>ByVal</font><font size=2> e </font><font color="#0000ff" size=2>As</font><font size=2> System.EventArgs) </font><font color="#0000ff" size=2>Handles</font><font size=2> Button1.Click<br><br></font><font color="#0000ff" size=2>Dim</font><font size=2> sb </font><font color="#0000ff" size=2>As</font> <font color="#0000ff" size=2>New</font><font size=2> System.Text.StringBuilder<br></font><font color="#0000ff" size=2>Dim</font><font size=2> LoopStart </font><font color="#0000ff" size=2>As</font> <font color="#0000ff" size=2>Integer</font><font size=2> = 0<br></font><font color="#0000ff" size=2>Dim</font><font size=2> LoopEnd </font><font color="#0000ff" size=2>As</font> <font color="#0000ff" size=2>Integer</font><font size=2> = 10<br></font><font color="#0000ff" size=2><br>Do<br></font><font color="#0000ff" size=2>  For</font><font size=2> x </font><font color="#0000ff" size=2>As</font> <font color="#0000ff" size=2>Integer</font><font size=2> = LoopStart </font><font color="#0000ff" size=2>To</font><font size=2> LoopEnd<br></font><font color="#008000" size=2>    'You can NOT change LoopStart or LoopEnd within<br></font><font color="#008000" size=2>    ' a FOR NEXT loop within Vb.Net<br></font><font size=2>    sb.Append(x.ToString &amp; </font><font color="#a31515" size=2>&quot; &quot;</font><font size=2>)<br></font><font color="#0000ff" size=2>  Next</font><font size=2> x<br>LoopEnd = LoopEnd - 1<br></font><font color="#0000ff" size=2>Loop</font> <font color="#0000ff" size=2>Until</font><font size=2> LoopStart = LoopEnd<br><br>MessageBox.Show(sb.ToString)<br></font><font color="#0000ff" size=2>End</font> <font color="#0000ff" size=2>Sub<br>End</font> <font color="#0000ff" size=2>Class<br><br><br><br><br></font>Regards,<br><br>John<br></p></font></font><hr size="1" align="left" width="25%">For links to VB.Net tutorials see here.&gt;&gt; http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835Mon, 03 Nov 2008 06:36:13 Z2008-11-03T06:36:13Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#549b5d10-3058-42cd-a0da-fd8168944b4bhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#549b5d10-3058-42cd-a0da-fd8168944b4bSJWhiteleyhttp://social.msdn.microsoft.com/Profile/en-US/?user=SJWhiteleyProblem in FOR loop ... Is it a BUG ?!!<div class=quote><font class=quoteHeader>The_knight5000 said:</font><p> mmm , I'm not talking about &quot;the actual 'For' statement&quot; ,it was in the code block !! and actually it raises OutOfRange exceptions when u use it like that with collections , or some logic errors just like above snippet !!<br></p><hr size=1 width="25%" align=left>MCP (Developing Web Applications ) MCP (Developing Windows Applications ) MCP (Designing and Implementing Databases with Microsoft® SQL Server™ 2000)<p></p></div><br>Iterating through a collection isn't the same as a for/next loop.<br><br>Your 'correct' loop probably arises from your familiarity with C style languages (which VB programmers may consider to be 'incorrect'). If you can answer this question &quot;<i>Which is the correct side of the road to drive a car?</i>&quot; you have answered your above question :)<br><hr size="1" align="left" width="25%">Stephen J WhiteleyMon, 03 Nov 2008 17:23:51 Z2008-11-03T17:23:51Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#b6803bcd-d269-4c7f-9e50-5022f3bdf3f8http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#b6803bcd-d269-4c7f-9e50-5022f3bdf3f8The_knight5000http://social.msdn.microsoft.com/Profile/en-US/?user=The_knight5000Problem in FOR loop ... Is it a BUG ?!!Good Evening,<br><br>Hey all , I'm not trying to avoid that or asking for alternative way to pass this situation in my code , I just asking why is For loop in VB.Net like that ? , and I'm wondering why do MS Developers Consist on that way also in the newest version ?!!<br><br><br>@<font class=postedBy><a href="../../../../../en-US/profile/?user=John Anthony Oliver&amp;referrer=http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f/" title="John Anthony Oliver">John Anthony Oliver</a></font><font class=affiliation></font>: really thanks for ur interest and I know I can't change the limits of the loop inside the loop block :s , and I still asking why ?!<br>btw , ur snippet does not solve the problem , It just involve code into nested loops !<br><font class=postedBy><br><br><a href="../../../../../en-US/profile/?user=SJWhiteley&amp;referrer=http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f/" title=SJWhiteley>SJWhiteley</a></font> :<br><div class=avatar><br></div><font class=affiliation></font><div class=quote><font class=quoteHeader>SJWhiteley said:</font><p></p>Iterating through a collection isn't the same as a for/next loop.<br><br>Your 'correct' loop probably arises from your familiarity with C style languages (which VB programmers may consider to be 'incorrect'). If you can answer this question &quot;<i>Which is the correct side of the road to drive a car?</i>&quot; you have answered your above question :)<br><hr align=left size=1 width="25%">Stephen J Whiteley<p></p></div>Ok man , It may be different  , But we don't care about what we are looping through , I talking here about the<b> FOR Statement</b> itself.<br><br>I think loops in Vb or C# or whatever the tool should use the same logic , and Actually i think this VB logic  in implementing for statement is not right .<br><br>when I'm talking about collections , I meant some thing like that :<br><br><br><br><div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:Courier New;font-size:11px"><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap"><tbody><tr><td><font style="font-size:11px">        </font><font style="color:blue">Dim</font><font style="font-size:11px"> arr </font><font style="color:blue">As</font><font style="font-size:11px"> </font><font style="color:blue">New</font><font style="font-size:11px"> ArrayList(4) </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">        arr.AddRange(<font style="color:blue">New</font><font style="font-size:11px"> </font><font style="color:blue">Integer</font><font style="font-size:11px">() {1, 2, 3, 4, 5}) </font></td></tr><tr><td>        <font style="color:blue">For</font><font style="font-size:11px"> i </font><font style="color:blue">As</font><font style="font-size:11px"> </font><font style="color:blue">Integer</font><font style="font-size:11px"> = 0 </font><font style="color:blue">To</font><font style="font-size:11px"> arr.Count - 1 </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">            Console.WriteLine(arr(i)) </td></tr><tr><td>            arr.RemoveAt(arr.Count - 1) </td></tr><tr><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">Next</font><font style="font-size:11px"> </font></td></tr></tbody></table></div><br>it should update the end limit (arr.count) every time it loops , but this does not happen , it catch the arr.Count at the first time only , and it will be not updated ..... why ?this is my questing and i think that is incorrect logic !!!!!!!<br><br><br>thanks <br><br><br><br><br><br><br><br><br><hr align=left size=1 width="25%">MHamdy<br> MCP (Developing Web Applications )<br> MCP (Developing Windows Applications )<br> MCP (Designing and Implementing Databases with Microsoft® SQL Server™ 2000)<br>Mon, 03 Nov 2008 18:28:58 Z2008-11-03T18:33:49Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#bf6584a3-fa96-4693-974f-de13bc7d5835http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#bf6584a3-fa96-4693-974f-de13bc7d5835SJWhiteleyhttp://social.msdn.microsoft.com/Profile/en-US/?user=SJWhiteleyProblem in FOR loop ... Is it a BUG ?!!VB has always performed loops like that. This implies a consistency. And honestly, consistency is a lot more important than correctness (whatever that may be perceived to be), in general.<br><br>As for your example, it demonstrates the consistency: once the loop is set, it is set. it <i>does </i>update the count, but in your example, as we have said, <i><b>the For/Next loop does not re-evaluate its bounds once you have established those bounds</b></i>.<br><br>I suspect, for the foreseeable future, this is how VB loops will always execute. Remember, you have a choice of languages; although they operate on the same framework there are quirks which differentiate them.<br><br>Really, asking for a change like this (breaking millions of applications) is not going to happen - until they remove the useless semicolons from C ;)<br><br>Seriously, though, this is quite a useful feature - it means you can perform a time consuming/complex operation to calculate your loop bounds (for example, establishing a record count in a dataset), without it having to be recalculated through each iteration.<br><br><i>For i as integer = ComplexOperationLower to ComplexOperationUpper<br>    ' Do Some More Stuff without reevaluating the ComplexOperationUpper or ComplexOperationLower<br>next</i><br><br>Or, if that doesn't satisfy you: it is what it is; Like it or lump it; If you can't stand the heat; horses for courses, etc. Personally, I use VB because I can't stand the quirks and inconsistencies of C.<br> <hr size="1" align="left" width="25%">Stephen J WhiteleyMon, 03 Nov 2008 20:35:28 Z2008-11-03T20:35:28Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#03b32968-90f7-466a-a018-ec1f7d863c78http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#03b32968-90f7-466a-a018-ec1f7d863c78dbasnetthttp://social.msdn.microsoft.com/Profile/en-US/?user=dbasnettProblem in FOR loop ... Is it a BUG ?!!sjw is correct, and the point about limit being a complicated formula is well taken.  if you need it both ways<br><br><div style="overflow:auto;background-color:white;line-height:100% ! important;font-family:Courier New;font-size:11px"><table style="border-width:0px;margin:2px 0px;width:99%;border-collapse:collapse;background-color:rgb(255, 255, 255)" cellpadding=0 cellspacing=0><col style="font-family:Courier New;font-size:11px;padding-left:10px;white-space:nowrap"><tbody><tr><td><font style="font-size:11px">        </font><font style="color:blue">Dim</font><font style="font-size:11px"> aY </font><font style="color:blue">As</font><font style="font-size:11px"> </font><font style="color:blue">Integer</font><font style="font-size:11px"> = 10 </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">        <font style="color:blue">Dim</font><font style="font-size:11px"> ax </font><font style="color:blue">As</font><font style="font-size:11px"> </font><font style="color:blue">Integer</font><font style="font-size:11px"> </font></td></tr><tr><td>        <font style="color:blue">For</font><font style="font-size:11px"> ax = 1 </font><font style="color:blue">To</font><font style="font-size:11px"> aY </font><font style="color:green">'tell For I want to do something 10 times</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">            Debug.WriteLine(ax.ToString()) </td></tr><tr><td>            aY -= 1<font style="color:green"><br></font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">            <font style="color:blue">If</font><font style="font-size:11px"> aY = 5 </font><font style="color:blue">Then</font><font style="font-size:11px"> </font><font style="color:blue">Exit</font><font style="font-size:11px"> </font><font style="color:blue">For</font><font style="font-size:11px"> </font><font style="color:green">'oh, but not really, i meant 5 times</font><font style="font-size:11px"> </font></td></tr><tr><td>        <font style="color:blue">Next</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">        <font style="color:green">'or</font><font style="font-size:11px"> </font></td></tr><tr><td>        <font style="color:blue">Dim</font><font style="font-size:11px"> aZ </font><font style="color:blue">As</font><font style="font-size:11px"> </font><font style="color:blue">Integer</font><font style="font-size:11px"> = 5 </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">        ax = 1 : aY = 10 </td></tr><tr><td>        <font style="color:blue">Do</font><font style="font-size:11px"> </font><font style="color:blue">While</font><font style="font-size:11px"> aY &lt;&gt; 0 </font><font style="color:blue">AndAlso</font><font style="font-size:11px"> aY &lt;&gt; aZ </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)">            Debug.WriteLine(ax.ToString) </td></tr><tr><td>            ax += 1 </td></tr><tr><td style="background-color:rgb(247, 247, 247)">            aY -= 1 </td></tr><tr><td>        <font style="color:blue">Loop</font><font style="font-size:11px"> </font></td></tr><tr><td style="background-color:rgb(247, 247, 247)"> </td></tr></tbody></table></div><br> Mon, 03 Nov 2008 21:47:09 Z2008-11-05T14:35:15Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#d51470f3-0905-483b-ae98-3fff1dd46575http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#d51470f3-0905-483b-ae98-3fff1dd46575John Anthony Oliverhttp://social.msdn.microsoft.com/Profile/en-US/?user=John%20Anthony%20OliverProblem in FOR loop ... Is it a BUG ?!!<div class=quote><font class=quoteHeader>SJWhiteley said:</font> <p>Or, if that doesn't satisfy you: it is what it is; <font style="background-color:#ffff00">Like it or lump it</font>; If you can't stand the heat; horses for courses, etc. Personally, I use VB because I can't stand the quirks and inconsistencies of C.<br> <hr align=left width="25%" size=1> Stephen J Whiteley <p></p></div> <p><br><br>Hi again The_knight5000,</p> <p>Despite what has been previously said, and you having 3 MCP qualifications, I have to agree with Stephen J Whiteley and anyone else who might write; &quot;<font style="background-color:#ffff00">Like it or lump it</font>&quot;<br><br>You can not honestly expect consistencies between <font style="background-color:#ffff00"><u><strong>ALL</strong></u></font> programming languages, surely?<br><br>If Visual Basic has always been this way since <strong><u>Visual Basic version 1.00</u></strong> , then why should Microsoft change things in Visual Basic? Besides the <font style="background-color:#ffff00">BASIC</font> programming language was invented in 1964 according to <a href="http://en.wikipedia.org/wiki/Basic_programming_language"><font style="background-color:#ccffff" color="#000000"><strong>http://en.wikipedia.org/wiki/Basic_programming_language</strong></font></a> so that makes it 44 years old ( 1 year older than me!! ).<br><br>C was created later in 1972 according to <a href="http://en.wikipedia.org/wiki/C_programming_language"><font style="background-color:#ccffff" color="#000000"><strong>http://en.wikipedia.org/wiki/C_programming_language</strong></font></a> some 8 years later.<br><br>What I am really getting that though is,<br><br>if <strong><u>FOR NEXT</u></strong> loops in <font style="background-color:#ffff00">BASIC</font> have always been like this <u><strong>then why should anyone</strong></u> change the <u>programming language specification</u> and introduce a <font style="font-size:16px;background-color:#ffff00"><strong><u>potential bug - problem</u></strong></font>?<br><br><br>Regards,<br><br>John<br></p><hr size="1" align="left" width="25%">For links to VB.Net tutorials see here.&gt;&gt; http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835Tue, 04 Nov 2008 01:10:37 Z2008-11-04T01:10:37Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#709b3879-f92d-419a-a2da-396d425ab0c8http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#709b3879-f92d-419a-a2da-396d425ab0c8The_knight5000http://social.msdn.microsoft.com/Profile/en-US/?user=The_knight5000Problem in FOR loop ... Is it a BUG ?!!Hello All<br><br><br><div class=quote><span class=quoteHeader></span><p></p><div class=quote><font class=quoteHeader>SJWhiteley said:</font> <p>Or, if that doesn't satisfy you: it is what it is; <font style="background-color:rgb(255, 255, 0)">Like it or lump it</font></p></div><p></p></div><div class=quote><span class=quoteHeader>John Anthony Oliver said:</span><p></p><a href="http://en.wikipedia.org/wiki/C_programming_language"><font style="background-color:rgb(204, 255, 255)" color="#000000"><strong></strong></font></a>if <strong><u>FOR NEXT</u></strong> loops in <font style="background-color:rgb(255, 255, 0)">BASIC</font> have always been like this <u><strong>then why should anyone</strong></u> change the <u>programming language specification</u> and introduce a <font style="font-size:16px;background-color:rgb(255, 255, 0)"><strong><u>potential bug - problem</u></strong></font>?<br><p></p></div><br>Actually I don't know what to say , that is really disappointing !!<br><br>Java , PHP , C , C# ... Implements Dynamic for Loop (as I can call) , and it is already stable and consistent !!<br>and you just should know this Static For loop in VB has a logic error !!<br><br>this is the reply of <i>Jonathan Aneja</i> on My Bug Report:<br><blockquote><blockquote><b>Hi there,<br><br>This behavior is by design, as documented in the help topics. VB has several different looping constructs that provide a lot of flexibility here - in this case you could use a &quot;Do While&quot; or &quot;Loop Until&quot; if you want the ability to dynamically change the upper bound.<br><br>I'm going to resolve this bug as &quot;by design&quot; for now, but if you have any questions feel free to contact me directly .<br><br>Thanks,<br><br>Jonathan Aneja<br>Program Manager, VB Team<br></b></blockquote></blockquote><br>thanks all , I just wanted to report about this !<br><br>regards, <br><br>MHamdy<br> <hr size="1" align="left" width="25%">MHamdy<br> MCP (Developing Web Applications )<br> MCP (Developing Windows Applications )<br> MCP (Designing and Implementing Databases with Microsoft® SQL Server™ 2000)<br>Wed, 05 Nov 2008 01:34:30 Z2008-11-05T01:34:30Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#2f950ddd-1600-4609-a8b7-765d54b9be16http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#2f950ddd-1600-4609-a8b7-765d54b9be16dbasnetthttp://social.msdn.microsoft.com/Profile/en-US/?user=dbasnettProblem in FOR loop ... Is it a BUG ?!!&quot;Actually I don't know what to say , that is really disappointing !!<br><br>Java , PHP , C , C# ... Implements Dynamic for Loop (as I can call) , and it is already stable and consistent !!<br>and you just should know this Static For loop in VB has a logic error !!&quot;<br><br>it is well documented (imagine reading the instructions), but so few do.  if you want the code to do what java, c, et al do then write in those languages<br><br>imho - for's are used to do something a specific amount of times.  what if the loop was hundred's of lines of code?  imagine debugging that.  the problem is really in your original construct, which probably should have been a do loop.<br><br>i got on my soap box once about 12:00 AM PM NOON MIDNIGHT, and even though i was correct, i lost. so my advice is do whatever you need to do to get over your disappointment, and move along.<br><br>&quot;<b>Give us grace to accept with serenity the things that cannot be changed, courage to change the things that should be changed, and the wisdom to distinguish the one from the other.&quot;</b> Wed, 05 Nov 2008 13:19:45 Z2008-11-05T13:25:14Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#264a5ac0-0eed-4630-afb5-13023970af84http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#264a5ac0-0eed-4630-afb5-13023970af84Dig-Boyhttp://social.msdn.microsoft.com/Profile/en-US/?user=Dig-BoyProblem in FOR loop ... Is it a BUG ?!!I think everyone has provided a broad, well-rounded explanation and discussion of the topic and there is little more to be squeezed from it.  From my perspective you can be clumped in with the masses who find fault with VB (or down-right hate it) because it does not do the things that their favorite language(s) do.  If you post this on the C# forum you might find an equal chorus willing to agree with you about this. Wed, 05 Nov 2008 13:41:03 Z2008-11-05T13:41:03Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#5387a375-690f-4f2c-a2a6-cb502a9a74d1http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#5387a375-690f-4f2c-a2a6-cb502a9a74d1John Anthony Oliverhttp://social.msdn.microsoft.com/Profile/en-US/?user=John%20Anthony%20OliverProblem in FOR loop ... Is it a BUG ?!!<div class=quote><font class=quoteHeader>dbasnett said:</font> <p>The problem is really in your original construct, which probably should have been a <font style="background-color:#ffffcc">do loop</font>.<br><br></p></div><br>Hi ALL,<br><br>I agree with this too.<br><br>You can achieve the same as the OP <strong><font style="background-color:#ffff00">The_knight5000</font></strong> was expecting like this.&gt;&gt;<br><br><font color="#0000ff" size=2> <p>Option</font> <font color="#0000ff" size=2>Strict</font> <font color="#0000ff" size=2>On<br><br>Public</font> <font color="#0000ff" size=2>Class</font><font size=2> Form1<br><br></font><font color="#0000ff" size=2>Private</font> <font color="#0000ff" size=2>Sub</font><font size=2> Button1_Click(</font><font color="#0000ff" size=2>ByVal</font><font size=2> sender </font><font color="#0000ff" size=2>As</font><font size=2> System.Object, </font><font color="#0000ff" size=2>ByVal</font><font size=2> e </font><font color="#0000ff" size=2>As</font><font size=2> System.EventArgs) </font><font color="#0000ff" size=2>Handles</font><font size=2> Button1.Click<br><br></font><font color="#0000ff" size=2>Dim</font><font size=2> sb </font><font color="#0000ff" size=2>As New </font><font size=2>System.Text.StringBuilder<br></font><font color="#0000ff" size=2>Dim</font><font size=2> Y </font><font color="#0000ff" size=2>As</font> <font color="#0000ff" size=2>Integer</font><font size=2> = 10<br></font><font color="#0000ff" size=2>Dim</font><font size=2> x2 </font><font color="#0000ff" size=2>As</font> <font color="#0000ff" size=2>Integer</font><font size=2> = 0<br><br><b><font size=2><font style="background-color:#000000"><font color="#33ff33">'The result you are after with a </font><font color="#ffff00">DO LOOP</font><font color="#33ff33">.&gt;&gt;</font></font></font></b><br></font><font color="#0000ff" size=2>Do<br></font><font size=2>sb.Append(x2.ToString &amp; Environment.NewLine)<br>x2 = x2 + 1<br>Y = Y - 1<br></font><font color="#0000ff" size=2>Loop</font> <font color="#0000ff" size=2>Until</font><font size=2> Y &lt; 5<br><br>MessageBox.Show(sb.ToString)<br><br><br></font><font size=2>sb = </font><font color="#0000ff" size=2>New</font><font size=2> System.Text.StringBuilder<br></font><font color="#0000ff" size=2>Dim</font><font size=2> x3 </font><font color="#0000ff" size=2>As</font> <font color="#0000ff" size=2>Integer</font><font size=2> = 0<br>Y = 10<br><br><b><font size=2><font style="background-color:#000000"><font color="#33ff33">'The result you are after with a </font><font color="#ffff00">WHILE</font><font color="#33ff33"> LOOP.&gt;&gt;<br></font></font></font></b></font><font color="#0000ff" size=2>While</font><font size=2> Y &gt; 4<br>sb.Append(x3.ToString &amp; Environment.NewLine)<br>x3 = x3 + 1<br>Y = Y - 1<br></font><font color="#0000ff" size=2>End</font> <font color="#0000ff" size=2>While<br></font><font size=2><br>MessageBox.Show(sb.ToString)<br><br></font><font color="#0000ff" size=2>End</font> <font color="#0000ff" size=2>Sub<br>End</font> <font color="#0000ff" size=2>Class</p></font> <hr align=left width="25%" size=1> For links to VB.Net tutorials see here.&gt;&gt; http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/29f2179b-997b-4115-a96d-a0834853b835Wed, 05 Nov 2008 14:22:05 Z2008-11-05T14:28:22Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#b0216a97-7142-4e62-8514-41d7d384d06fhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#b0216a97-7142-4e62-8514-41d7d384d06fdbasnetthttp://social.msdn.microsoft.com/Profile/en-US/?user=dbasnettProblem in FOR loop ... Is it a BUG ?!!i already posted a do loop example, in a lot less space ;) Wed, 05 Nov 2008 14:34:22 Z2008-11-05T14:34:22Zhttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#5e1b2b77-8231-4d20-bd31-1945391a8e0ehttp://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/e7b47ed3-834e-4e05-8357-00ca92fc8a1f#5e1b2b77-8231-4d20-bd31-1945391a8e0eSJWhiteleyhttp://social.msdn.microsoft.com/Profile/en-US/?user=SJWhiteleyProblem in FOR loop ... Is it a BUG ?!!Thankfully, these kind of 'discussions' pop up a lot less than they used to with VB6/C++.<br><br>Historically (and currently?) VBers get very defensive when 'their' language is attacked for being wrong, and C programmers tend to see themselves as better for using an obviously superior language.<br><br>Unfortunately, it'll probably always be this way: VB has a lot of baggage associated with it. Some are just quirks, some are actually better and more logical methodologies compared to C, and some should just go away (but won't).<br><br>With VB and C# being so comparable, the differences generally fall down to the quirks and taste. Additionally, as has been posted, there is more than one way to do a specific task.<br><br> <hr size="1" align="left" width="25%">Stephen J WhiteleyWed, 05 Nov 2008 16:31:34 Z2008-11-05T16:31:34Z