473,441 Members | 1,877 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,441 software developers and data experts.

Collections ByVal ByRef?

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.
Nov 17 '05 #1
4 5466
Hi,

Well, you do not explain your real escenario very well, so I hope this
help.

A collection is always passed by ref, therefore if you change it inside a
method it will be reflected outside, even more, if you are iterating in a
collection you should not change it, it would rise an exception.
you can change the members of the collected instances though.

where u keep the count variable? from the call to checkforqtyoftools it
seems that you keep it inside either box , or t

maybe a better way to do it is to use a hashtable, like this:

Hashtable table = new Hashtable()
foreach( tool t in box)
if ( table[ t.name] == null )
table[ t.name] =1;
else
table[ t.name] = (int)table[ t.name] + 1;
much easier and clear.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"aaj" <aa*@aaj.com> wrote in message
news:1118401150.a806c9dff696a25fb3ac5680706cb348@t eranews...
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.

Nov 17 '05 #2
aaj
Thanks Ignacio

I didn't realise it always passed byref (although that makes sense).

I never change the values in the 'inside itteration', only test against
them, which probably explains how I've gotten it to work so far.8-)

I'll take a look at hashtables, and see if it works better in this case

thanks again

Andy
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:%2****************@TK2MSFTNGP15.phx.gbl...
Hi,

Well, you do not explain your real escenario very well, so I hope this
help.

A collection is always passed by ref, therefore if you change it inside a
method it will be reflected outside, even more, if you are iterating in a
collection you should not change it, it would rise an exception.
you can change the members of the collected instances though.

where u keep the count variable? from the call to checkforqtyoftools it
seems that you keep it inside either box , or t

maybe a better way to do it is to use a hashtable, like this:

Hashtable table = new Hashtable()
foreach( tool t in box)
if ( table[ t.name] == null )
table[ t.name] =1;
else
table[ t.name] = (int)table[ t.name] + 1;
much easier and clear.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"aaj" <aa*@aaj.com> wrote in message
news:1118401150.a806c9dff696a25fb3ac5680706cb348@t eranews...
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.


Nov 17 '05 #3
Hi Andy,

if your class implements IEnumrable it has to implement the GetEnumerator
method, wich should return an IEnumerator.
That Enumerator should contain the counter not the collection, and the
Enumerator should always be a new instance.
Then there will be one counter per loop.

Christof

"aaj" <aa*@aaj.com> schrieb im Newsbeitrag
news:1118401150.a806c9dff696a25fb3ac5680706cb348@t eranews...
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.

Nov 17 '05 #4
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
Well, you do not explain your real escenario very well, so I hope this
help.

A collection is always passed by ref


No it's not. "Pass by reference" and "pass reference by value" are
significantly different.

See http://www.pobox.com/~skeet/csharp/parameters.html

(I know you know the full story, Ignacio, but I don't think it helps
people to give the simplified but incorrect version.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

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

Similar topics

8
by: Sandy | last post by:
Hello! Help!!!! I have ten zillion books that attempt to describe the difference between ByVal and ByRef and none of them are clear to me. I have gathered that ByVal makes a copy and ByRef...
7
by: Hei | last post by:
Hi, i know the difference of ByRef and ByVal, in case if use byref or byval don't affect the result which one should prefer? (less memory use, better performance ....issue) thx
4
by: Carlos Gomez | last post by:
In VB6 the default for passing variables was ByRef. It was faster and used less memory. Why did MS changed that? Are there any advantages using ByVal over ByRef? (other than ByVal impeding you from...
14
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered...
14
by: Niklas | last post by:
Hi What I have learned is that a variable is just a reference when dealing with Objects. Are you supposed to use ByVal or ByRef in functions? They produce the same result or have I missed...
4
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive,...
7
by: barrett bonden | last post by:
Is there any way to pass parameters to a function and simply know there will get there without the silly (C like ) complexity of worring about byval and or perhaps byref ? (Why bother to...
2
by: Witold Iwaniec via .NET 247 | last post by:
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net? Here is sample code from sample Asp.Net...
8
by: Boni | last post by:
Dear all, I found out that I don' understand byVal/byRef in VB. There is a simple example: Why in the first test the result is 10,10 where in the second 0,20. Thanks for your help. Boni Module...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.