473,323 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Looping function

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
Jun 27 '08 #1
10 1257
Charlie Brown wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #2
On 2008-06-13, Charlie Brown <cb****@duclaw.comwrote:
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
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
Jun 27 '08 #3
On 2008-06-13, Göran Andersson <gu***@guffa.comwrote:
Charlie Brown wrote:
>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.
Yeah, I didn't even consider the performance implications!

--
Tom Shelton
Jun 27 '08 #4
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...

"Charlie Brown" wrote:
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
Jun 27 '08 #5
Tom,
>>
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.

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
Jun 27 '08 #6
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
Jun 27 '08 #7
Cor Ligthert[MVP] wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #8
On 2008-06-14, Cor Ligthert[MVP] <no************@planet.nlwrote:
Tom,
>>>
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.

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
It's the number of times the loop runs, and the question of wether it's
necessary or not. Somethies, Cor, there are better ways to do things that can
improve performance.

--
Tom Shelton
Jun 27 '08 #9
>
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

Jun 27 '08 #10
On Jun 14, 1:58*pm, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nl>
wrote:
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
@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.
Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David | last post by:
Hello. I am looking for advice on what is "best practice" regarding looping through a form to check its checkboxes and associated data fields. Here is what I am trying to do (Here is the page...
2
by: Ivo | last post by:
Hi, I have an audio file (.mid or .wav or .mp3) in an object element: <object id="snd" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"...
5
by: Bruce Lawrence | last post by:
I'm running Access 97 and my modules are looping if someone puts an invalid value in. The setup: 3 macros - get_clock_num, verify_clocknum, append_to_history 3 functions. each in their own...
5
by: masood.iqbal | last post by:
My simplistic mind tells me that having local variables within looping constructs is a bad idea. The reason is that these variables are created during the beginning of an iteration and deleted at...
2
by: John Dalberg | last post by:
In a traditional for loop, one can use the index (say i) and continue looping in another function and come back to original for loop (even though it's not good practice) Can the newer foreach...
0
by: anthon | last post by:
Hi all - first post! anywho; I need to create a function for speeding up and down a looping clip. imagine a rotating object, triggered by an action, and slowly decreasing in speed, till it...
4
by: seninfothil | last post by:
hai i wrote a program for sudoku puzzle .... for that i need to go for recursion function . inside the function i have go for looping.... where i have call the rec..function again.. ...
2
by: pedalpete | last post by:
Hi All, I'm using Bill Scott's YUI carousel v5.6, and I've run into a crazy looping javascript for some reason, and I can't seem to get around it. The carousel loads a bunch of items, 7 of...
3
by: chiku1523 | last post by:
Hi, Please find the following code. In function setAnswers, I am looping with each question. I have inner loop, which is looping for each answers of the questions. If any of the answer for question...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.