Need C# Class method that persists information already in Object

I have created a class in my ASP.Net project called LitHoldModifications. Here's the code:

[Serializable]
public class LitHoldModifications
{
    private Boolean _changed;
    private Hashtable _added;
    private Hashtable _deleted;

    public Boolean Changed
    {
        get { return _changed; }
        set { _changed = value; }
    }

    public Hashtable Added
    {
        get { return _added; }
        set { _added = value; }
    }

    public Hashtable Deleted
    {
        get { 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 is that I need to be able to Add multiple items to an instance of this class. The code I have to do this is (in an aspx page):

public LitHoldModifications AffectedEmployeeModifications
    {
        get
        {
            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 I run my code and get to ProcessAffectedviaJavascript(), the string[] employees is populated with multiple names, but each time the code gets to the line AffectedEmployeeModifications.Add.... a new Hashtable is created and returned by the Add method, so any earlier strings from employees that have been added to AffectedEmployeeModifications are lost. The only way I can think to get around this is to change the Add method to take AffectedEmployeeModifications as a parameter and do this:

public Hashtable Add(Hashtable lhm, String item1, String item2)
    {
        lhm.Add(item1, item2);
        return lhm;
    }

and then, in my aspx.cs:

AffectedEmployeeModifications = AffectedEmployeeModifications.Add(AffectedEmployeeModifications, Anumber, AName);

This doesn't seem very OOP-y though, and my OOP skills are clearly wanting. How should I do this?

Answers


You should be constructing the objects that your class needs in a constructor for the class. You should add a default constructor like this:

public LitHoldModifications() 
{
    Added = new Hashtable();
    Deleted = new Hashtable();
}

Then you can remove the assignments in your Add() and Delete() methods, because you can assume in these methods that those member variables will already be valid.

I would also recommend changing your Add/Delete methods to returning void. If you want access to the Added/Deleted hashtables, you can just reference that property instead.

var myLitInstance = new LitHoldModifications();
myLitInstance.Add("value1", "value2");
Hashtable tbl = myLitInstance.Added;

Need Your Help

Parse boolean arithmetic including parentheses with regex?

javascript python regex

Is there a single regular expression that can parse a string (in Python and/or Javascript, does not need to be the same expression) that represents simple boolean arithmetic? For example I want to ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.