循环功能 [英] Looping function

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

问题描述

我正在使用类似以下函数的东西来循环我正在处理的

食谱程序。当我写这篇文章时,我开始认为这根本不会起作用......但不是我不太确定。

这会是代码工作,还是给我带来问题?


Private Sub DoSomething()

Dim viCount As Integer = _varianceList.Count

For我作为整数= 0到viCount - 1

Dim vi As ProductionVarianceItem = _varianceList(i)

vi.ProductionOut = GetProductionOut(vi,_varianceList)

下一页

结束子


私人函数GetProductionOut(ByVal item As

ProductionVarianceItem,ByVal itemList As List(Of

ProductionVarianceItem))作为Double

Dim itemCount As Integer = itemList.Count

For i As Integer = 0 to itemCount - 1

Dim vi As ProductionVarianceItem = itemList(i)


''做点什么


下一页

结束功能

I am using something like the following function to loop through a
recipe program I am working on. As I was writing it, I starting
thinking this isn''t going to work at all... but not I''m not so sure.
Will this code work, or cause me problems?

Private Sub DoSomething()
Dim viCount As Integer = _varianceList.Count
For i As Integer = 0 To viCount - 1
Dim vi As ProductionVarianceItem = _varianceList(i)
vi.ProductionOut = GetProductionOut(vi, _varianceList)
Next
End Sub

Private Function GetProductionOut(ByVal item As
ProductionVarianceItem, ByVal itemList As List(Of
ProductionVarianceItem)) As Double
Dim itemCount As Integer = itemList.Count
For i As Integer = 0 To itemCount - 1
Dim vi As ProductionVarianceItem = itemList(i)

''do something

Next
End Function

推荐答案

Charlie Brown写道:
Charlie Brown wrote:

我正在使用类似以下函数的东西来循环我正在处理的

食谱程序。当我写这篇文章时,我开始认为这根本不会起作用......但不是我不太确定。

这会是代码工作,还是给我带来问题?


Private Sub DoSomething()

Dim viCount As Integer = _varianceList.Count

For我作为整数= 0到viCount - 1

Dim vi As ProductionVarianceItem = _varianceList(i)

vi.ProductionOut = GetProductionOut(vi,_varianceList)

下一页

结束子


私人函数GetProductionOut(ByVal item As

ProductionVarianceItem,ByVal itemList As List(Of

ProductionVarianceItem))作为Double

Dim itemCount As Integer = itemList.Count

For i As Integer = 0 to itemCount - 1

Dim vi As ProductionVarianceItem = itemList(i)


''做点什么


下一页

结束功能
I am using something like the following function to loop through a
recipe program I am working on. As I was writing it, I starting
thinking this isn''t going to work at all... but not I''m not so sure.
Will this code work, or cause me problems?

Private Sub DoSomething()
Dim viCount As Integer = _varianceList.Count
For i As Integer = 0 To viCount - 1
Dim vi As ProductionVarianceItem = _varianceList(i)
vi.ProductionOut = GetProductionOut(vi, _varianceList)
Next
End Sub

Private Function GetProductionOut(ByVal item As
ProductionVarianceItem, ByVal itemList As List(Of
ProductionVarianceItem)) As Double
Dim itemCount As Integer = itemList.Count
For i As Integer = 0 To itemCount - 1
Dim vi As ProductionVarianceItem = itemList(i)

''do something

Next
End Function



您要做的是什么?


该代码会循环两个项目的所有组合,包括当然

将每个项目与自身相结合。所以,如果你有100件商品,那就是做什么?b $ b $代码将运行10000次。


-

G?ran Andersson

_____
http://www.guffa.com


On 2008-06-13,Charlie Brown< cb **** @ duclaw.comwrote:
On 2008-06-13, Charlie Brown <cb****@duclaw.comwrote:

我正在使用类似以下函数的东西来循环播放

食谱程序我正在研究。当我写这篇文章时,我开始认为这根本不会起作用......但不是我不太确定。

这会是代码工作,还是给我带来问题?


Private Sub DoSomething()

Dim viCount As Integer = _varianceList.Count

For我作为整数= 0到viCount - 1

Dim vi As ProductionVarianceItem = _varianceList(i)

vi.ProductionOut = GetProductionOut(vi,_varianceList)

下一页

结束子


私人函数GetProductionOut(ByVal item As

ProductionVarianceItem,ByVal itemList As List(Of

ProductionVarianceItem))作为Double

Dim itemCount As Integer = itemList.Count

For i As Integer = 0 to itemCount - 1

Dim vi As ProductionVarianceItem = itemList(i)


''做点什么


下一页

结束功能
I am using something like the following function to loop through a
recipe program I am working on. As I was writing it, I starting
thinking this isn''t going to work at all... but not I''m not so sure.
Will this code work, or cause me problems?

Private Sub DoSomething()
Dim viCount As Integer = _varianceList.Count
For i As Integer = 0 To viCount - 1
Dim vi As ProductionVarianceItem = _varianceList(i)
vi.ProductionOut = GetProductionOut(vi, _varianceList)
Next
End Sub

Private Function GetProductionOut(ByVal item As
ProductionVarianceItem, ByVal itemList As List(Of
ProductionVarianceItem)) As Double
Dim itemCount As Integer = itemList.Count
For i As Integer = 0 To itemCount - 1
Dim vi As ProductionVarianceItem = itemList(i)

''do something

Next
End Function



嗯... _varianceList似乎是一个成员变量,所以如果GetProductionOut

仅适用于该列表,那么似乎没有意义将它传递给参数

list - 因为无论如何它都可以直接访问它。那么,如果是这种情况(

从上下文中不清楚),那么我可能会改变

GetProductionOut的签名,如下所示:


私人功能GetProductionOut(ByVal item As ProductionVarianceItem)As Double

''做东西

结束功能


至于循环,很难说。

你的循环在技术上没有任何问题 - 但是,有更好的方法可以做到这一点。但是,它最终将取决于代码试图完成的内容。第一个循环向我看来

它可以更清楚地写成For Each:


Public Sub DoSomething()

对于每个vi As ProductionVarianceItem在_varianceList中

vi.ProductionOut = GetProductionOut(vi)

下一页

结束子

即使我打算做一个for循环(比每个
稍快一点),那么我很可能会这样写:


Public Sub DoSomething()

For i As Integer = 0 To _varianceList.Count - 1

vi.ProductionOut = GetProductionOut(_varianceList(i))

下一页

结束子


这同样适用于GetProductionOut函数。除非你需要在循环之后使用我的最终值,然后你也可以在

循环中声明它,这样你就缩小了我的范围它是最狭隘的用途。


无论如何,就像我说的那样 - 似乎没有任何技术错误

带你的循环或任何有理由相信他们不会工作......


-

Tom Shelton

Well... _varianceList appears to be a member variable, so if GetProductionOut
only works on that list, then it seems pointless to pass it in the arguments
list - since it can directly access it anyway. So, if that''s the case (which
is unclear from the context) then I would probably change the signature of
GetProductionOut to be something like:

Private Function GetProductionOut (ByVal item As ProductionVarianceItem) As Double
'' do stuff
End Function

As for the looping, it''s hard to say. There is nothing technically wrong with
your loops - but, there maybe better ways of doing this. But, it ultimately
depends on what the code is trying to accomplish. The first loop looks to me
that it could be written more clearly as a For Each:

Public Sub DoSomething ()
For Each vi As ProductionVarianceItem In _varianceList
vi.ProductionOut = GetProductionOut(vi)
Next
End Sub

Even if I was going to do a for loop (which is slightly faster then a for
each), then I would most likely write it like this:

Public Sub DoSomething ()
For i As Integer = 0 To _varianceList.Count - 1
vi.ProductionOut = GetProductionOut(_varianceList(i))
Next
End Sub

The same applies to the GetProductionOut function. Unless you need to use the
final value of i after the loop - then you might as well declare it in the
loop, so that you are reducing the scope of i to it''s narowest use.

Anyway, like I said - there doesn''t appear to be anything technically wrong
with your loops or any reason to believe that they won''t "work"...

--
Tom Shelton


2008-06-13,G?ran Andersson< gu *** @ guffa.comwrote:
On 2008-06-13, G?ran Andersson <gu***@guffa.comwrote:

Charlie Brown写道:
Charlie Brown wrote:

>我正在使用类似以下函数的东西来循环我正在处理的
食谱程序。在我写作的时候,我开始认为这根本不会起作用......但不是我不太确定。
这段代码会起作用,还是会引起我的问​​题?

Private Sub DoSomething()
Dim viCount As Integer = _varianceList.Count
对于i as Integer = 0到viCount - 1
Dim vi As ProductionVarianceItem = _varianceList( i)
vi.ProductionOut = GetProductionOut(vi,_varianceList)
下一页

私人函数GetProductionOut(ByVal item As
ProductionVarianceItem,ByVal itemList作为List(Of
ProductionVarianceItem))As Double
Dim itemCount As Integer = itemList.Count
对于i as Integer = 0 To itemCount - 1
Dim vi As ProductionVarianceItem = itemList( i)

''做点什么

下一页
结束功能
>I am using something like the following function to loop through a
recipe program I am working on. As I was writing it, I starting
thinking this isn''t going to work at all... but not I''m not so sure.
Will this code work, or cause me problems?

Private Sub DoSomething()
Dim viCount As Integer = _varianceList.Count
For i As Integer = 0 To viCount - 1
Dim vi As ProductionVarianceItem = _varianceList(i)
vi.ProductionOut = GetProductionOut(vi, _varianceList)
Next
End Sub

Private Function GetProductionOut(ByVal item As
ProductionVarianceItem, ByVal itemList As List(Of
ProductionVarianceItem)) As Double
Dim itemCount As Integer = itemList.Count
For i As Integer = 0 To itemCount - 1
Dim vi As ProductionVarianceItem = itemList(i)

''do something

Next
End Function



是什么给你的正在尝试做什么?


该代码会循环播放将两个项目组合在一起,当然包括将每个项目与自身相结合的
。所以,如果你有100件商品,那就是做什么?b $ b $代码将运行10000次。


What is it that you are trying to do?

That code would loop all combinations of two items, including of course
combining each item with itself. So, if you have 100 items the "do
something" code will run 10000 times.



是的,我甚至没有考虑性能影响!


- < br $>
Tom Shelton

Yeah, I didn''t even consider the performance implications!

--
Tom Shelton


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

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