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

modify list value with foreach iteration variable

Bob
Hi,
I have a list of widgets.
I want to iterate through the selected items collection and modify one of
the widgets properties.
I tried
foreach(widget w in lst.SelectedItems)
w.MyProperty = something;
but I get a message saying "Cannot modify members of w because it is a
foreach iteration variable"
What is the point of iteration if you can't modify the objects?
Thanks
Bob
May 24 '06 #1
2 8777
"Bob" <bo*@nowhere.com> wrote:
foreach(widget w in lst.SelectedItems)
w.MyProperty = something;
but I get a message saying "Cannot modify members of w because it is a
foreach iteration variable"
What is widget? What is lst? What is MyProperty?
What is the point of iteration if you can't modify the objects?


You can modify the objects. You can't modify the collection, though. If
modifying the object would cause it to modify the collection, then that
isn't allowed.

I think your complaint would be more meaningful if it was precise!

-- Barry

--
http://barrkel.blogspot.com/
May 24 '06 #2
Bob
Hi Barry,
Thanks for your reply.
Problem solved. My mistake was assuming that a listbox items collection
could have it members modified.
regards
Bob

"Barry Kelly" <ba***********@gmail.com> wrote in message
news:kn********************************@4ax.com...
"Bob" <bo*@nowhere.com> wrote:
foreach(widget w in lst.SelectedItems)
w.MyProperty = something;
but I get a message saying "Cannot modify members of w because it is a
foreach iteration variable"


What is widget? What is lst? What is MyProperty?
What is the point of iteration if you can't modify the objects?


You can modify the objects. You can't modify the collection, though. If
modifying the object would cause it to modify the collection, then that
isn't allowed.

I think your complaint would be more meaningful if it was precise!

-- Barry

--
http://barrkel.blogspot.com/

May 24 '06 #3

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

Similar topics

1
by: jwpioneer | last post by:
I have a need within an application to modify the path environment variable, as I need to find specific directories and remove them. I use the following code to do this: RegistryKey rkey = ...
27
by: Tripper | last post by:
Which is the better way to go and why? //trivial example List<string> strings = GetStrings(); foreach (string s in strings) { // some operation; } strings.ForEach(
15
by: Macca | last post by:
Hi, My app needs to potentially store a large number of custom objects and be able to iterate through them quickly. I was wondering which data structure would be the most efficient to do this,a...
2
by: james | last post by:
(Sorry if this is a double post) Hi Guys, I am creating a delegate a couple times and passing in a local variable. However, when the delegate is invoked it uses the variable passed into the...
44
by: Zytan | last post by:
The docs for List say "The List class is the generic equivalent of the ArrayList class." Since List<is strongly typed, and ArrayList has no type (is that called weakly typed?), I would assume...
4
by: bg_ie | last post by:
Hi, What is wrong with the following - short arrfile = new short; foreach (short s in arrfile) { s = 10; }
2
by: John Kelsey | last post by:
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...
8
by: =?Utf-8?B?YmJn?= | last post by:
I am getting this error when I tried to modify one field inside foreach loop. public struct myStruct { public int a; public bool b; //... } private List<myStructMyStruct = new...
4
RMWChaos
by: RMWChaos | last post by:
The next episode in the continuing saga of trying to develop a modular, automated DOM create and remove script asks the question, "Where should I put this code?" Alright, here's the story: with a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.