ref keyword in foreach statement??

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I received this error when I used "ref" in foreach statement.

Dictionary<string, object> oInterfaces;

foreach (clsInterface vinterface in oInterfaces.Values)
{
func1( ref vinterface );
}

Error: Cannot pass 'vinterface' as a ref or out argument because it is a
'foreach iteration variable'


I tried to use for statement but it's seem that "vinterface =
oInterfaces[0];" isn't correct. Because the index "i" isn't a key.

for (int i = 0; i < oInterfaces.Count; i++)
{
vinterface = oInterfaces;
func1( ref vinterface );
}

Any ideas?

thanks,
ano
 
Well, what are you trying to do? What is oInterfaces?

It looks like you are trying to reassign the elements of .Values; so,
without knowing what .Values /is/, it is hard to know what the API for
maintaining it is; from the fact that foreach works and doesn't, I'm
guessing that it is simply IEnumerable or IEnumerable<T>, but these
interfaces only expose the read-only enumerable aspect. If you want to
reassign the objects, then you will need to know an interface with a
writeable indexer or similar (e.g. .SetValue(key|index)).

However: first verify that you really do need to reassign the item in the
collection. "Ref" parameters are often used incorrectly by people who think
they need to use "ref" to update the object (as opposed to cloning it and
only updating the clone); for reference-type values (classes) this is not
the case (For value-type values (structs) you /may/ need to use something
like this, although a return value would be another viable option).

If the "ref" is purely to satisfy the API of func1(), and you don't actually
need to reassign the value in oInterfaces, then you could also use:

foreach (clsInterface vinterface in oInterfaces.Values)
{
clsInterface temp = vinterface;
func1( ref temp);
}

Hope this helps; otherwise post more info ;-p

Marc
 
ano said:
I received this error when I used "ref" in foreach statement.

Dictionary<string, object> oInterfaces;

foreach (clsInterface vinterface in oInterfaces.Values)
{
func1( ref vinterface );
}

Error: Cannot pass 'vinterface' as a ref or out argument because it is a
'foreach iteration variable'


I tried to use for statement but it's seem that "vinterface =
oInterfaces[0];" isn't correct. Because the index "i" isn't a key.

for (int i = 0; i < oInterfaces.Count; i++)
{
vinterface = oInterfaces;
func1( ref vinterface );
}

Any ideas?

thanks,
ano


Hi ano,

Do you *need* to pass it as ref? Not to be patronising, but one of the
common misunderstandings about ref is that "You need to pass an object as
ref if you want to modify it in a method." Not true! You pass the object
in, and you can modify away, because objects are passed around as
references. The correct statement about 'ref' is: "You need to pass a
*variable* as ref if you want to reassign to that variable."

Do you _really_ need ref?

(Man, what a cool tagline)
 
Oh right, you did answer one of my questions in the OP - I just missed it.
In which case you can do everything you want (even the ref) in a roundabout
way by enmerating a copy of the keys (see below). Not as direct, but it
works.

However, as per my previous post (and Tom's), I urge you to double-check
that you really need the "ref" access...

Marc


public class SomeClass { } // dummy
static void Main() {
Dictionary<string, SomeClass> dict = new Dictionary<string,
SomeClass>();
dict["abc"] = new SomeClass();
dict["def"] = new SomeClass();
dict["ghi"] = new SomeClass();
dict["jkl"] = new SomeClass();

int count = dict.Count;
string[] keys = new string[count];
dict.Keys.CopyTo(keys, 0);
foreach (string key in keys) {
SomeClass value = dict[key];
SomeMethod(ref value);
dict[key] = value;
}

}
static void SomeMethod(ref SomeClass item) {
item = new SomeClass();
}
 
Hi Tom,

I'm not sure that I really need "ref" or not?
Now I try to convert VB project (not my project) to C#.
In VB, there are some "ByRef". I'm don't know much about VB so I don't know
exactly what the program does.


Tom Spink said:
ano said:
I received this error when I used "ref" in foreach statement.

Dictionary<string, object> oInterfaces;

foreach (clsInterface vinterface in oInterfaces.Values)
{
func1( ref vinterface );
}

Error: Cannot pass 'vinterface' as a ref or out argument because it is a
'foreach iteration variable'


I tried to use for statement but it's seem that "vinterface =
oInterfaces[0];" isn't correct. Because the index "i" isn't a key.

for (int i = 0; i < oInterfaces.Count; i++)
{
vinterface = oInterfaces;
func1( ref vinterface );
}

Any ideas?

thanks,
ano


Hi ano,

Do you *need* to pass it as ref? Not to be patronising, but one of the
common misunderstandings about ref is that "You need to pass an object as
ref if you want to modify it in a method." Not true! You pass the object
in, and you can modify away, because objects are passed around as
references. The correct statement about 'ref' is: "You need to pass a
*variable* as ref if you want to reassign to that variable."

Do you _really_ need ref?

(Man, what a cool tagline)
 
ano said:
I received this error when I used "ref" in foreach statement.

Dictionary<string, object> oInterfaces;

foreach (clsInterface vinterface in oInterfaces.Values)
{
func1( ref vinterface );
}

Error: Cannot pass 'vinterface' as a ref or out argument because it is a
'foreach iteration variable'


I tried to use for statement but it's seem that "vinterface =
oInterfaces[0];" isn't correct. Because the index "i" isn't a key.

for (int i = 0; i < oInterfaces.Count; i++)
{
vinterface = oInterfaces;


vinterface = oInterfaces.Values;
 
Back
Top