modify list value with foreach iteration variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob

    modify list value with foreach iteration variable

    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.SelectedIte ms)
    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


  • Barry Kelly

    #2
    Re: modify list value with foreach iteration variable

    "Bob" <bob@nowhere.co m> wrote:
    [color=blue]
    > foreach(widget w in lst.SelectedIte ms)
    > w.MyProperty = something;
    > but I get a message saying "Cannot modify members of w because it is a
    > foreach iteration variable"[/color]

    What is widget? What is lst? What is MyProperty?
    [color=blue]
    > What is the point of iteration if you can't modify the objects?[/color]

    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

    --

    Comment

    • Bob

      #3
      Re: modify list value with foreach iteration variable

      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" <barry.j.kelly@ gmail.com> wrote in message
      news:kns772lnko lad8ilvt04nfq0l sqp1hpet1@4ax.c om...[color=blue]
      > "Bob" <bob@nowhere.co m> wrote:
      >[color=green]
      > > foreach(widget w in lst.SelectedIte ms)
      > > w.MyProperty = something;
      > > but I get a message saying "Cannot modify members of w because it is a
      > > foreach iteration variable"[/color]
      >
      > What is widget? What is lst? What is MyProperty?
      >[color=green]
      > > What is the point of iteration if you can't modify the objects?[/color]
      >
      > 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/[/color]


      Comment

      Working...