1

第一件事。我有以下课程:

class Employee
{
    private int employeeID;
    private string firstName;
    private string lastName;
    private bool eligibleOT;
    private int positionID;
    private string positionName;
    private ArrayList arrPhone;
    public IList<Sector> ArrSector {get; private set;}

    //the constructor method takes in all the information of the employee
    public Employee(int empID, string fname, string lname, bool elOT, int pos, string posname)
    {
        employeeID = empID;
        firstName = fname;
        lastName = lname;
        eligibleOT = elOT;
        positionID = pos;
        positionName = posname;
        arrPhone = new ArrayList();
        ArrSector = new List<Sector>();
    }

    //the constructor method takes in the employee id, the first name and the last name of the employee
    public Employee(int empid, string firstname,string lastname)
    {
        employeeID = empid;
        firstName = firstname;
        lastName = lastname;
    }

    //overtides the first name and the last name as a string.
    public override string ToString()
    {
        return firstName +" "+lastName;
    }



    public int EmployeeID
    {
        get { return employeeID; }
        set { employeeID = value; }
    }

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    public string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }

    public bool EligibleOT
    {
        get { return eligibleOT; }
        set { eligibleOT = value; }
    }

    public int PositionID
    {
        get { return positionID; }
        set { positionID = value; }
    }

    public string PositionName
    {
        get { return positionName; }
        set { positionName = value; }
    }

    public ArrayList ArrPhone
    {
        get { return arrPhone; }
        set { arrPhone = value; }
    }



    // The function assigns all the variables associated to the employee to a new object.
    public static object DeepClone(object obj)
    {
        object objResult = null;
        using (MemoryStream ms = new MemoryStream())
        {
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(ms, obj);
            ms.Position = 0;
            objResult = bf.Deserialize(ms);
        }
        return objResult;
    }

   //Memento pattern is used to save the employee state.
   //The changes will be rolled back if the update button not clicked
    public class Memento : IMemento
    {
        private Employee originator = null;
        private int employeeID;
        private string firstName;
        private string lastName;
        private bool eligibleOT;
        private int positionID;
        private string positionName;
        private ArrayList arrPhone;
        private IList<Sector> arrSector;

        public Memento(Employee data)
        {
            this.employeeID = data.EmployeeID;
            this.firstName = data.FirstName;
            this.lastName = data.LastName;
            this.eligibleOT = data.EligibleOT;
            this.positionID = data.PositionID;
            this.positionName = data.PositionName;
            this.arrPhone = data.ArrPhone;

            this.originator = data;
            this.arrSector = Extensions.Clone<Sector>(data.ArrSector);
        }

}

我在winforms中使用升C。我的应用程序的前端在左侧有一个列表框,其中包含员工的名字。在左侧,有不同的文本框对应于列表框中选择的员工。我以这样一种方式对其进行了编码,每次我选择一个员工时,它的属性,如员工 ID、姓名、职位等都会显示在这些字段中。

如果用户更改员工的任何属性,他必须单击更新按钮才能对数据库进行更改。现在真正的问题是,当用户更改所选员工的任何字段并选择另一个员工而不单击更新按钮时,我想显示一个弹出框告诉用户如果他选择另一个员工,所有更改都将是丢失。

出于这个原因,我创建了 momento 类来保存员工的先前状态。我也尝试过重载 == 运算符

        public static bool operator ==(Employee e, Memento m)
        {
            return ((e.employeeID == m.employeeID) &&
               (e.firstName == m.firstName) &&
               e.lastName == m.lastName &&
               e.eligibleOT == m.eligibleOT &&
               e.positionID == m.positionID &&
               e.positionName == m.positionName &&
               e.arrPhone == m.arrPhone &&
               e.ArrSector == m.arrSector);
        }

        public static bool operator !=(Employee e, Memento m)
        {
            return (e.employeeID != m.employeeID);
        }

我的想法是比较这两个对象......但我没有成功。我该怎么做?如果进行了更改,我如何显示弹出窗口。?我在哪里放置代码以显示弹出窗口?

4

2 回答 2

2

一个警告......在您的==!=运算符中使用不同的逻辑通常不是一个好主意。能够同时拥有两者有点==!=直观false

if(!(a == b) && !(a != b))
{
    // head explodes
}

除此之外,我猜您在比较代码中将您的Employee类引用为object(或其他父类)。也许是这样的:

if(listBox1.SelectedItem != currentMemento)
{
    ...
}

如果是这种情况,则编译器不会将 绑定!=到您的自定义实现。强制listBox1.SelectedItem转换Employee为强制。

if((Employee)listBox1.SelectedItem != currentMemento)
{
    ...
}

但是,您可以采取许多其他方法来解决此问题:

  • 完全在 GUI 端实现实现,bool设置为true文本字段中的数据更改时,然后在更改员工时检查该标志
  • 实现IComparableorIEquatable接口
  • 覆盖和/或类Equals上的方法EmployeeMemento

(如果您选择第二个选项,通常建议您完成第三个)

例子

这是您可以做的一个示例(我假设您有一个ListBox命名listBox1并且您已使用该函数附加到SelectedIndexChanged事件listBox1_SelectedIndexChanged):

private Employee lastSelectedEmployee;
private Memento selectedMemento;

void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    Employee selectedEmployee = (Employee)listBox1.SelectedItem;

    if(lastSelectedEmployee != null && lastSelectedEmployee != selectedEmployee)
    {
        if(/*changes exist*/)
        {
            if(/*cancel changes*/)
            {
                listBox1.SelectedItem = lastSelectedEmployee;

                return;
            }
        }
    }

    lastSelectedEmployee = selectedEmployee;
    selectedMemento = //create the memento based on selectedEmployee;
}

您必须为我留下评论的区域提供自己的逻辑,但这个想法应该非常简单。

于 2010-01-28T13:48:53.280 回答
-1

看看 IComparable 接口。它要求您实现不需要进行此类比较的方法。知识库文章,希望它为你变成英语,在我的电脑上它总是变成德语。

-sa

于 2010-01-28T13:36:46.543 回答