chr*_*ris 7 vb.net asp.net iterator repeater .net-2.0
我有许多意识中继器,我需要遍历所有项目.我目前有:
For Each item In rpt1.Items
...do some stuff
Next
For Each item In rpt2.Items
...exact same code
Next
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以将其减少为单个For Each ... Next循环?
编辑:"做一些事情"涉及到许多本地变量,这就是为什么我不能只将项目传递给函数 - 调用必须包含大约8个ByRef参数.
只需将您的代码放入一个单独的方法中即可:
For Each item In rpt1.Items
DoSomethingWith(item)
Next
For Each item In rpt2.Items
DoSomethingWith(item)
Next
...
Sub DoSomethingWith(item As RepeaterItem)
... put your common code here ...
End Sub
Run Code Online (Sandbox Code Playgroud)
编辑:如果你有很多局部变量,使用局部 lambda 可能是一个选择:
Dim doSomething = Sub(item As RepeaterItem)
... do some stuff using all available local variables
End Sub
For Each item In rpt1.Items
doSomething(item)
Next
For Each item In rpt2.Items
doSomething(item)
Next
Run Code Online (Sandbox Code Playgroud)
编辑:还有一个选项,不需要 lambda:
For Each rpt In New Repeater() {rpt1, rpt2}
For Each item In rpt.Items
...do something with item and rpt
Next
Next
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3035 次 |
| 最近记录: |