oop - Need C# Class method that persists information already in Object -


i have created class in asp.net project called litholdmodifications. here's code:

[serializable] public class litholdmodifications {     private boolean _changed;     private hashtable _added;     private hashtable _deleted;      public boolean changed     {         { return _changed; }         set { _changed = value; }     }      public hashtable added     {         { return _added; }         set { _added = value; }     }      public hashtable deleted     {         { return _deleted; }         set { _deleted = value; }     }      public hashtable add(string item1, string item2)     {         added = new hashtable();         added.add(item1, item2);         return added;     }      public hashtable delete(string item1, string item2)     {         deleted = new hashtable();         deleted.add(item1, item2);         return deleted;     } } 

the problem i'm having need able add multiple items instance of class. code have (in aspx page):

public litholdmodifications affectedemployeemodifications     {                 {             if (viewstate["affectedemployeemodifications"] != null)                 return (litholdmodifications)viewstate["affectedemployeemodifications"];             else                 return null;         }          set         {             viewstate["affectedemployeemodifications"] = value;         }     }  protected void processaffectedviajavascript()     {        string[] employees = hiddenemployeespopup.value.split('|');        if (employees.length>1) {         foreach (string s in employees)         {             if (s.length > 1)             {                  string anumber = s.split('@')[0];                 string aname = s.split('@')[1];                  listitem item = new listitem();                 item.text = aname;                 item.value = anumber;                 lstselemployees.items.add(item);                  //clear values in temp hidden field:                 hiddenemployeespopup.value = "";                  affectedemployeemodifications.add(anumber, aname);                 affectedemployeemodifications.changed = true;              }           }         } 

when run code , processaffectedviajavascript(), string[] employees populated multiple names, each time code gets line affectedemployeemodifications.add.... new hashtable created , returned add method, earlier strings employees have been added affectedemployeemodifications lost. way can think around change add method take affectedemployeemodifications parameter , this:

public hashtable add(hashtable lhm, string item1, string item2)     {         lhm.add(item1, item2);         return lhm;     } 

and then, in aspx.cs:

affectedemployeemodifications = affectedemployeemodifications.add(affectedemployeemodifications, anumber, aname); 

this doesn't seem oop-y though, , oop skills wanting. how should this?

you should constructing objects class needs in constructor class. should add default constructor this:

public litholdmodifications()  {     added = new hashtable();     deleted = new hashtable(); } 

then can remove assignments in add() , delete() methods, because can assume in these methods member variables valid.

i recommend changing add/delete methods returning void. if want access added/deleted hashtables, can reference property instead.

var mylitinstance = new litholdmodifications(); mylitinstance.add("value1", "value2"); hashtable tbl = mylitinstance.added; 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -