重置foreach循环。 [英] Reset a foreach loop.

查看:83
本文介绍了重置foreach循环。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法重置foreach循环来重新遍历

集合,就好像它从头开始一样?也就是说,如果我从一个集合中删除一个项目

,我希望能够重置循环。


-

-----------------------------------

Ken Varn
高级软件工程师

Diebold Inc.


EmailID = varnk

Domain = Diebold.com
-----------------------------------

Is there any way to reset a foreach loop to re-iterate through the
collection as if it were starting from the beginning? Namely, if I delete
an item out of a collection, I want to be able to reset the loop.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

推荐答案

" Ken Varn" < NOSPAM>在新闻中写道:up ************** @ TK2MSFTNGP14.phx.gbl:
"Ken Varn" <nospam> wrote in news:up**************@TK2MSFTNGP14.phx.gbl:
有没有办法重置foreach循环重新遍历<收集好像是从一开始就开始的?也就是说,如果我从一个集合中删除一个项目,我希望能够重置
循环。
Is there any way to reset a foreach loop to re-iterate through the
collection as if it were starting from the beginning? Namely, if I
delete an item out of a collection, I want to be able to reset the
loop.




不,你需要使用for循环或while循环。

-

Chad Z. Hower(又名Kudzu) - http://www.hower.org/Kudzu/

编程是一种反击的艺术形式 ;


使用IntraWeb授权ASP.NET
http://www.atozed.com/IntraWeb/


" Ken Varn" < NOSPAM>在留言中写道

新闻:上传************** @ TK2MSFTNGP14.phx.gbl ...
"Ken Varn" <nospam> wrote in message
news:up**************@TK2MSFTNGP14.phx.gbl...
有没有办法重置一个foreach循环以重新遍历
集合,就好像它从头开始一样?也就是说,如果我从一个集合中删除一个项目,我希望能够重置循环。
Is there any way to reset a foreach loop to re-iterate through the
collection as if it were starting from the beginning? Namely, if I delete
an item out of a collection, I want to be able to reset the loop.




我不得不说我不同意for循环遍历集合

然后更改循环中的集合。但是如果你必须这样做,

将for循环包裹在while循环中(while(1 = 1))并且不会结束。


while(1 == 1)

{

foreach(...)

{

if( item_deleted_from_collection)

{

item_deleted = true;

休息;

}

}

if(not item_deleted)

休息;

}


HTH,

Mike Rodriguez



I have to say I disagree with a for loop that iterates through a collection
and then changes the collection in the loop. However if you must do it,
wrap the for loop inside of a while loop (while (1=1)) that doesn''t end.

while (1==1)
{
foreach (...)
{
if (item_deleted_from_collection)
{
item_deleted = true;
break;
}
}
if (not item_deleted)
break;
}

HTH,

Mike Rodriguez


好奇,你想要完成的最终结果是什么?

也许还有一种更好的方法。

" Ken Varn" < NOSPAM>在留言中写道

新闻:上传************** @ TK2MSFTNGP14.phx.gbl ...
Just curious, what is the end result that you are trying to accomplish?
Perhaps there is a better way.
"Ken Varn" <nospam> wrote in message
news:up**************@TK2MSFTNGP14.phx.gbl...
有没有办法重置一个foreach循环以重新遍历
集合,就好像它从头开始一样?也就是说,如果我从一个集合中删除一个项目,我希望能够重置循环。

-
---------- -------------------------
Ken Varn
高级软件工程师
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
------------------------------- ----
Is there any way to reset a foreach loop to re-iterate through the
collection as if it were starting from the beginning? Namely, if I delete
an item out of a collection, I want to be able to reset the loop.

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------



这篇关于重置foreach循环。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆