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

Confused... "Cannot pass 'Item' as a ref or out argument because it is a 'foreach iteration variable'"

I am an old, longtime C programmer surprised and confused by an error
message I'm getting from my VS2005 compiler...

"Cannot pass 'Item' as a ref or out argument because it is a 'foreach
iteration variable'" is the error message I'm getting for code that I think
should work. I'm sure that there is some sublety I'm missing... maybe with
the use of a reference, or foreach, or template class. Can someone maybe
explain it with small words or point me to something to read?

Example code:
using System;
using System.Collections.Generic;
namespace Test
{
class test
{
public int x = 1;
}

class Demo
{
List<testBasicList = new List<test>();
public Demo()
{
BasicList.Add(new test());
foreach (test Item in BasicList)
{
Double(ref Item); // <---compiler error here
}
}
public void Double(ref test Item)
{
Item.x *= 2;
}
}
}

// In the old days, I'd use pointers and a for loop and it would work. Can
anyone help me sort it out in my head?
Aug 24 '07 #1
2 19766
On Aug 24, 3:34 pm, "John Kelsey" <kels...@tacits.comwrote:
I am an old, longtime C programmer surprised and confused by an error
message I'm getting from my VS2005 compiler...

"Cannot pass 'Item' as a ref or out argument because it is a 'foreach
iteration variable'" is the error message I'm getting for code that I think
should work. I'm sure that there is some sublety I'm missing... maybe with
the use of a reference, or foreach, or template class. Can someone maybe
explain it with small words or point me to something to read?
"Foreach" variables are readonly, which means you can't pass them by
reference.

However, my guess is that you don't understand what "ref" really
means. See
http://pobox.com/~skeet/csharp/parameters.html

Jon

Aug 24 '07 #2
It's due to the way foreach works.
The reference Item is readonly so you can't have it point to a different
object.

On the other hand you don't need to use ref in the first place, since you're
working with a reference type already.

If you remove the ref keywords from your code it'll work as you expect it
to. (as long as what your expecting is for x to become 2 :-))
"John Kelsey" <ke*****@tacits.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>I am an old, longtime C programmer surprised and confused by an error
message I'm getting from my VS2005 compiler...

"Cannot pass 'Item' as a ref or out argument because it is a 'foreach
iteration variable'" is the error message I'm getting for code that I
think should work. I'm sure that there is some sublety I'm missing...
maybe with the use of a reference, or foreach, or template class. Can
someone maybe explain it with small words or point me to something to
read?

Example code:
using System;
using System.Collections.Generic;
namespace Test
{
class test
{
public int x = 1;
}

class Demo
{
List<testBasicList = new List<test>();
public Demo()
{
BasicList.Add(new test());
foreach (test Item in BasicList)
{
Double(ref Item); // <---compiler error here
}
}
public void Double(ref test Item)
{
Item.x *= 2;
}
}
}

// In the old days, I'd use pointers and a for loop and it would work.
Can anyone help me sort it out in my head?
Aug 24 '07 #3

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

Similar topics

5
by: DraguVaso | last post by:
Hi, Something I don't understand about a Typed DataSet: When a value in the DataSet is DBNull, it throws this error: "Cannot get value because it is DBNull". But aren't Typed DataSets...
2
by: Mr.KisS | last post by:
Hello. I'm under Windows XP PRO SP1, IIS 5.1 ans SQL SERVER 2005 Express. When i try to open a connexion with : <connectionStrings> <add name="AppCnxStr"...
9
by: Igor Okulist | last post by:
int func(void**); { short* p = NULL; func(&p); //<<< here } Could somebody remind me why is this not allowed ? error message: "cannot convert parameter from 'short **' to 'void **'"
2
by: Florian G. Pflug | last post by:
Hi Since sometime yesterday, my postgresql (7.4.5) reports "ERROR: cannot compare arrays of different element types", when I analyze a specific table in my database. Here is the tables...
0
by: shengmin.ruan | last post by:
i got a struct like: --------------------- public ref struct OutPacket{ int m_replyIndex; List<MainInfo^>^ m_mainInfos; }; --------------------- when i pass it to another process, i have...
0
by: DongWook | last post by:
Dear all, I've a windows application with crystal viewer to show a report (using vb .net 2003). I made a setup project and installed on client machines (no .net and crystal report). The...
2
by: mahesh | last post by:
Can anyone direct me to the place where i find the solution for the error message "cannot call member function 'X' without object"??? thanks in advance
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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.