c# - Edit specific item from list -


i ask how can edit item collection. example: in collection mylist (string name, datetime event). make event moved next year.

i tried:

 public class classevent     {         public string name { get; set; }          public datetime date { get; set; }   public classevent(string name, datetime date)         {              name = name;             date = date;         } 

myclass:

public list<classevent> event= new list<classevent>(); public void addevent(string name, datetime myevent)         {             classevent  ev = new classevent(name, myevent.date);             event.add(ev);         } 

mainform:

forach( var in myclass.event) {    if(a.myevent == datetime.today)       {            myclass.addevent(a.name, a.myevent.addyears(1); } 

but doesnt work.

you can't modify collection within foreach loop. use for loop isntead:

to add new items collection:

  // pay attention: backward loop   for(int = myclass.event.count - 1; >= 0; --i) {     classevent = myclass.event[i];      if (a.myevent == datetime.today)       myclass.addevent(a.name, a.myevent.addyears(1);   } 

to change existing items in collection:

  // direction forward/backward   for(int = myclass.event.count - 1; >= 0; --i) {     classevent = myclass.event[i];      // change "a", since "a" item in collection     //todo: check syntax; "myevent" expected being writeable property      if (a.myevent == datetime.today)         a.myevent = a.myevent.addyears(1);   } 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -