而在VB.NET中循环

cjo*_*136 0 vb.net while-loop

我有一些语法问题.我习惯了C#,刚刚开始使用VB.NET.我试图while循环从列表中的项目循环.我做错了什么语法?

While oResponse.outputControl.Items(i) <> Nothing

    //Do something

End While
Run Code Online (Sandbox Code Playgroud)

Joe*_*orn 5

不要忘记增加你的柜台:

While oResponse.outputControl.Items(i) <> Nothing

    'Do something
    i += 1

End While
Run Code Online (Sandbox Code Playgroud)

如果这是一个引用类型(你没说,但它可能是),你无法将它Nothing<>运算符进行比较:

While oResponse.outputControl.Items(i) IsNot Nothing

    'Do something
    i += 1

End While
Run Code Online (Sandbox Code Playgroud)

但也许你真正想要的是For Each循环:

For Each Item In oResponse.outputControl.Items
    'Do Something
Next Item
Run Code Online (Sandbox Code Playgroud)

还有一件事:oResponse变量中的匈牙利疣是什么?不再推荐这种风格,微软现在甚至特别推荐它.VB.Net还有一个名为"默认属性"的功能,这可能会使这更简单.将它们全部拉到一起(现在包括上面评论中的ListItem类型):

For Each Item As ListItem In Response.outputControl
    'Do Something
Next Item
Run Code Online (Sandbox Code Playgroud)