C# 比较更新前后的两种对象状态

C# 比较更新前后的两种对象状态,c#,.net-3.5,class,memento,C#,.net 3.5,Class,Memento,第一件事。 我有以下课程: class Employee { private int employeeID; private string firstName; private string lastName; private bool eligibleOT; private int positionID; private string positionName; private ArrayList arrPhone; public

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

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);
        }
我的想法是比较两个物体。。。
但是我没有成功。如何操作??如果进行了更改,如何显示弹出窗口。?我将代码放置在何处以显示弹出窗口?

查看IComparable界面。它要求您实现您不需要进行这种比较的方法,希望它能为你变成英语,在我的电脑上它总是变成德语


-sa

请查看IComparable界面。它要求您实现您不需要进行这种比较的方法,希望它能为你变成英语,在我的电脑上它总是变成德语


-sa

一个警告词…在
==
中使用不同的逻辑通常不是一个好主意=运算符。同时拥有
==
有些不直观=
be
false
同时

if(!(a == b) && !(a != b))
{
    // head explodes
}
除此之外,我猜您的
Employee
类在比较代码中被引用为
对象(或其他父类)。也许是这样的:

if(listBox1.SelectedItem != currentMemento)
{
    ...
}
如果是这种情况,那么编译器没有绑定
=到您的自定义实现。强制转换列表框1。选择编辑项
到员工
,以强制执行该操作

if((Employee)listBox1.SelectedItem != currentMemento)
{
    ...
}
但是,您可以采取许多其他方法来解决此问题:

  • 完全在GUI端实现,当文本字段中的数据更改时,使用
    bool
    设置为
    true
    ,然后在更改员工时检查该标志
  • 实现
    i可比较
    i可比较
    接口
  • 覆盖
    Employee
    和/或
    Memento
    类上的
    Equals
    方法
(如果您选择第二个选项,通常建议您也完成第三个选项)

示例

下面是一个您可以执行的示例(我假设您有一个名为
listBox1
ListBox
,并且您已使用
listBox1\u SelectedIndexChanged
函数附加到
SelectedIndexChanged
事件):


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

一句警告……在你的
===
中使用不同的逻辑通常不是一个好主意=运算符。同时拥有
==
有些不直观=
be
false
同时

if(!(a == b) && !(a != b))
{
    // head explodes
}
除此之外,我猜您的
Employee
类在比较代码中被引用为
对象(或其他父类)。也许是这样的:

if(listBox1.SelectedItem != currentMemento)
{
    ...
}
如果是这种情况,那么编译器没有绑定
=到您的自定义实现。强制转换列表框1。选择编辑项
到员工
,以强制执行该操作

if((Employee)listBox1.SelectedItem != currentMemento)
{
    ...
}
但是,您可以采取许多其他方法来解决此问题:

  • 完全在GUI端实现,当文本字段中的数据更改时,使用
    bool
    设置为
    true
    ,然后在更改员工时检查该标志
  • 实现
    i可比较
    i可比较
    接口
  • 覆盖
    Employee
    和/或
    Memento
    类上的
    Equals
    方法
(如果您选择第二个选项,通常建议您也完成第三个选项)

示例

下面是一个您可以执行的示例(我假设您有一个名为
listBox1
ListBox
,并且您已使用
listBox1\u SelectedIndexChanged
函数附加到
SelectedIndexChanged
事件):


您必须为我留下的注释提供您自己的逻辑,但想法应该非常简单。

MSDN:IComparable)接口定义CompareTo方法,该方法确定实现类型实例的排序顺序。IEquatable)接口定义Equals方法,该方法确定实现类型的实例的相等性。如果比较的对象相等,则CompareTo的返回值为0。因此,您可以使用IComparable来测试相等性。一个副作用是排序的比较。感谢您投票否决功能性答案。虽然我没有投票否决,但
IComparable
界面是为排序和相对比较而设计的,而不是专门为相等性检查而设计的。在2.0之前,它是平等性检查的主要接口,但2.0引入了
IEquatable
接口(以及
IEqualityComparer
接口来补充
IComparer
),它不要求实现能够确定哪个实例“更大”,不管两个实例是否等效。MSDN:IComparable)接口定义CompareTo方法,该方法确定实现类型实例的排序顺序。IEquatable)接口定义Equals方法,该方法确定实现类型的实例的相等性。如果比较的对象相等,则CompareTo的返回值为0。因此,您可以使用IComparable来测试相等性。一个副作用是排序的比较。感谢您投票否决功能性答案。虽然我没有投票否决,但
IComparable
界面是为排序和相对比较而设计的,而不是专门为相等性检查而设计的。在2.0之前,它是平等性检查的主要接口,但2.0引入了
IEquatable
接口(以及
IEqualityComparer
接口来补充
IComparer
),它不要求实现能够确定哪个实例“更大”,不管两个实例是否相等