A
aaj
Hi all
can anyone shed a little light on to my Newbie problem?
I have written my own collection that impliements the IEnumerator/Enumerable
interfaces and on the surface, all seems well.
The problem happens while I Itterate through it using foreach
during the itteration I recursively itterate the same collection. Although
what follows is not the code I'm using, it simplifies the question I'm tring
to ask.
if I have a collection of tools in a box
foreach tool t in box
{
checkforqtyoftools(box,t.name)
}
and then I have a function that for each toot, runs through itsself again
counting the amount of times it occurs
checkforqtyoftools(collection box, tool toolname)
foreach tool t in box
{
count number of tools.name in box....
}
my collection class uses a variable 'count' to hold the position within the
collection.
The problem is, even though the default passing is byval, the inner loop is
altering the count variable of the 'box' object of the outer loop,
The obvious answer is to store the count at the beggining of the inner loop,
and set it back as it exits, but this doesn't feel like a good way to do it.
So the questions are
why does passing a collection byval to a function alow the varaibles in the
collection to be altered.
What is the best way of dealing with the problem.
many thanks
Andy
PS I know one of the answers might be you dont need to itterate int the
other loop, do it the other way... but my problem is different from the one
above, its more the principle of keeping the two collections seperate that
I'm interested in.
can anyone shed a little light on to my Newbie problem?
I have written my own collection that impliements the IEnumerator/Enumerable
interfaces and on the surface, all seems well.
The problem happens while I Itterate through it using foreach
during the itteration I recursively itterate the same collection. Although
what follows is not the code I'm using, it simplifies the question I'm tring
to ask.
if I have a collection of tools in a box
foreach tool t in box
{
checkforqtyoftools(box,t.name)
}
and then I have a function that for each toot, runs through itsself again
counting the amount of times it occurs
checkforqtyoftools(collection box, tool toolname)
foreach tool t in box
{
count number of tools.name in box....
}
my collection class uses a variable 'count' to hold the position within the
collection.
The problem is, even though the default passing is byval, the inner loop is
altering the count variable of the 'box' object of the outer loop,
The obvious answer is to store the count at the beggining of the inner loop,
and set it back as it exits, but this doesn't feel like a good way to do it.
So the questions are
why does passing a collection byval to a function alow the varaibles in the
collection to be altered.
What is the best way of dealing with the problem.
many thanks
Andy
PS I know one of the answers might be you dont need to itterate int the
other loop, do it the other way... but my problem is different from the one
above, its more the principle of keeping the two collections seperate that
I'm interested in.