首页 > 解决方案 > VB.NET:部分代码根本不执行

问题描述

我在 VB.NET 中有包含两个嵌套 For 语句的代码。例如

For i = 0 to ListBox1.Items.Count - 1
 MsgBox("First For statement")
 For x = 5 to 1
  MsgBox("Second For statement")
 Next
Next

现在第一个 For 语句执行得很好,但第二个没有。怎么来的?代码不应该逐行执行吗?

标签: vb.netfor-loop

解决方案


Vb.net 中的 For 循环语法:

For counter [ As datatype ] = start To end [ Step step ]
[ statements ]
[ Continue For ]
[ statements ]
[ Exit For ]
[ statements ]
Next [ counter ]

您错过了代码中的步骤

For x = 5 to 1 step -1
   MsgBox("Second For statement")
Next

推荐阅读