我刚刚转到Visual Basic团队工作.
breakVisual Basic中的等效关键字是什么,即提前退出循环而不是方法?
我正在使用C#.我有一个项目清单.我使用一个循环遍历每个项目foreach.在我的内心,我foreach有很多if陈述检查一些东西.如果这些if语句中的任何一个返回false,那么我希望它跳过该项并转到列表中的下一个项.if应忽略以下所有陈述.我尝试使用休息但是休息时间退出整个foreach声明.
这就是我目前拥有的:
foreach (Item item in myItemsList)
{
if (item.Name == string.Empty)
{
// Display error message and move to next item in list. Skip/ignore all validation
// that follows beneath
}
if (item.Weight > 100)
{
// Display error message and move to next item in list. Skip/ignore all validation
// that follows beneath
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢
为了论证,我怎么能在VB中这样做?
foreach foo in bar
{
if (foo == null)
break;
if (foo = "sample")
continue;
// More code
//...
}
Run Code Online (Sandbox Code Playgroud) 我已将以下方法添加到Array原型中:
Array.prototype.foreach = function(func){
for(var i = 0; i < this.length; i++){
if(!func(this[i]) === false) break; //return false from func in order to break the loop
}
return this;
}
Run Code Online (Sandbox Code Playgroud)
在同一个文件中,在上面的代码之后,我有以下jQuery插件:
jQuery.fn.addClassForEvents = function(){
var that = this;
arguments.foreach(function(event){
that.bind(event[0], function(){
that.addClass(event[0]);
})
.bind(event[1], function(){
that.removeClass(event[0]);
});
});
return this;
}
Run Code Online (Sandbox Code Playgroud)
为了使用这个jQuery插件,我的代码看起来像:
$('div').addClassForEvents(['mouseenter', 'mouseleave']);
Run Code Online (Sandbox Code Playgroud)
但是,浏览器在jQuery插件的"arguments.foreach(...."行中抛出一个错误,简单地说明了
对象#没有方法'foreach'
然而,该foreach方法适用于我的代码的其他地方.为什么在这个jQuery插件中未定义?
让我们采取以下代码片段:
int i = 0;
while ( i <= 10 )
{
System.out.println(i);
if ( i == 8 )
{
continue;
}
i++;
}
Run Code Online (Sandbox Code Playgroud)
我必须在代码中进行哪些更改以避免无限循环?
c# ×2
vb.net ×2
arrays ×1
asp.net ×1
continue ×1
exit ×1
if-statement ×1
java ×1
javascript ×1
jquery ×1
loops ×1
prototype ×1
statements ×1
vb6 ×1
while-loop ×1