需要C#Class方法来保存已经存在于Object中的信息

时间:2013-05-02 16:57:22

标签: c# oop

我在我的ASP.Net项目中创建了一个名为LitHoldModifications的类。这是代码:

[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;
    }
}

我遇到的问题是我需要能够将多个项目添加到此类的实例中。我必须这样做的代码是(在aspx页面中):

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;

            }

         }

       }

当我运行我的代码并进入ProcessAffectedviaJavascript()时,字符串[]雇员填充了多个名称,但每次代码到达AffectedEmployeeModifications.Add ...行时,都会创建一个新的Hashtable并返回Add方法,因此已添加到AffectedEmployeeModifications的员工的任何早期字符串都将丢失。我能想到的唯一方法是更改​​Add方法以将AffectedEmployeeModifications作为参数并执行此操作:

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

然后,在我的aspx.cs中:

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

虽然这看起来并不是很好,而且我的OOP技能显然很缺乏。我该怎么做?

1 个答案:

答案 0 :(得分:5)

您应该在类的构造函数中构造类所需的对象。您应该添加如下默认构造函数:

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

然后您可以删除Add()和Delete()方法中的赋值,因为您可以在这些方法中假设这些成员变量已经有效。

我还建议您将添加/删除方法更改为返回void。如果您想要访问已添加/已删除的哈希表,则可以改为引用该属性。

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