Looping function

  • Thread starter Thread starter Charlie Brown
  • Start date Start date
C

Charlie Brown

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 said:
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

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.
 
It appears to me that the value of productionOut for each item in the array
depends on the properties of all the objects in the array. Therefore, it
seems to me at least potentially incorrect if later an entry was changed that
would somehow effect the value of an already computed productionOut.

In essence, what I mean is, suppose you compute a value of _varianceList
(5).productionOut = 7.3. If the computation of _varianceList
(3).productionOut needed the value of _varianceList(5).productionOut, then it
would have (possibly) had a different value. Therefore the computation of
_varianceList(3) is in doubt, etc, etc...

I don't know your subject or problem definition to know if this is a problem
or not however...
 
Tom,
Yeah, I didn't even consider the performance implications!

--

Not about the methods, but about your message.

In my idea is this very short as it is about bitmap processing
I can really not see any performance implications.

Cor
 
Charlie,

One of those loops is as I see it completely withouth sense.

As I see it right they both do exactly the same

Cor
 
Cor said:
Charlie,

One of those loops is as I see it completely withouth sense.

As I see it right they both do exactly the same

Cor

No, they don't do the same thing at all. One loop is within the other.

I don't know if that is what's intended (that's why I asked what he's
trying to do), but that's what the code does.
 
No, they don't do the same thing at all. One loop is within the other.
Yea, but in my idea is the one inside the other doing the same loop
completely again

Cor
 
Yea, but in my idea is the one inside the other doing the same loop
completely again

Cor

@Everyone
Thanks for everyone's input.

Yes, it does look a little ridiculous looping through itself, but here
is the situation. I have a list of recipes that have been
"produced" (i.e. made by people each day). Let's say, today the
restaurant has made 32 Qt's of Spicy BBQ Sauce. One of the
ingredients of Spicy BBQ sauce is just plain BBQ Sauce. BBQ sauce is
also in the list because 10 QT's of it were made for other reasons.

So I need to loop through the list and find any items that have
ingredients that are also on the same list and then add the right
amount to them, so I know the total made.

In the end, I used something very similar to what i posted, it seems
to working fine, list total count is about 500 and won't creep to much
higher than that in the foreseeable future.

@Tom Shelton
I use the For... Next instead of For Each for performance sake,
although I agree it's probably not much to worry about. As for the
referencing of the count outside of the loop, that's there for legacy
sake, and will get tossed during the next big iteration when i have
time to replace all of them, for now I like to keep the code
consistent.

@Family Tree Mike
Exactly. You seem to have hit the nail on the head about what the
code does. The biggest problem is that some ingredients are in
mutliple recipes. And those recipes can also be ingredients in other
recipes. There is some pretty deep nesting involved, nearly 4 levels
in some places. This increases the complexity of the loops and does
indeed change the .ProductionOut values quite often in the middle of
the loop.
 
Back
Top