Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 575405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:56:13+00:00 2026-05-13T13:56:13+00:00

first things first. I have the following classes: class Employee { private int employeeID;

  • 0

first things first.
I have the following classes:

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

}

I am using C sharp in winforms. the front end of my application has a listbox on the left end side which has the first name of the employee.on the left hand side, there are different textboxes which correspond to the employee selected in the list box. I have coded it in such a way that everytime i select an employee, its attributes, like the employee id, name, position, etc are displayed in these fields.

if the user changes any attribute of the employee, he has to click an update button to make the changes to the database.
now the real problem, when the user changes any field of the selected employee, and selects another employee without clicking the update button, i want to show a pop up box to tell the user that if he selects another employee , all the changes will be lost.

for this reason i have created the momento class to hold the previous state of the employee.
i have also tried overloading the == operator

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

my idea was to compare the two object…
but m not successfull. how do i do it??how do i show the popup if changes are made.?where do i place the code to show the popup?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:56:14+00:00Added an answer on May 13, 2026 at 1:56 pm

    One word of warning…it’s generally not a good idea to have different logic in your == and != operators. It’s somewhat unintuitive to be able to have both == and != be false at the same time.

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

    That aside, I’m guessing that you have your Employee class referenced as an object (or other parent class) in your comparison code. Maybe something like this:

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

    If this is the case, then the compiler isn’t binding the != to your custom implementation. Cast listBox1.SelectedItem to Employee in order to force that.

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

    There are, however, many other approaches that you could take to solve this issue:

    • Make the implementation entirely on the GUI side, with a bool that gets set to true when the data in the text fields changes, then check that flag when changing employees
    • Implement the IComparable or IEquatable interfaces
    • Override the Equals method on the Employee and/or Memento class

    (If you go with the second option, it’s generally recommended that you complete the third as well)

    Example

    Here’s an example of what you could do (I’m assuming you have a ListBox named listBox1 and you’ve attached to the SelectedIndexChanged event with the listBox1_SelectedIndexChanged function):

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

    You’ll have to provide your own logic for the areas I’ve left comments, but the idea should be pretty straightforward.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all i have to clear few things 1.This is not spam group
I have an app with a UITextField, amongst other things. When the user first
One of the first things I learned in C++ was that #include <iostream> int
I am working on my first NHibernate project, and have the following setup/requirement. I
Can somebody explain to me why classes are not first class objects in Java?
I have been learning Objective-C as my first language and understand Classes, Objects, instances,
We have a lot of class code which has some boilerplate like the following:
Say I have two classes: class A(db.Model): class B(db.Model): a_reference = ReferenceProperty(A) I can
I have some jQuery/JS below. The first thing to run is the alert box
One of the first things I like to do when I make a site

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.