How can I duplicate a class object using C#

Pat Hanks 141 Reputation points
2023-02-10T21:39:32.04+00:00

I have a class object that I use on my form to contain the data from my database named Item.cs. The issue I am having is when I am attempting to duplicate or make a copy of this object to another all the data is cleared. Once I execute the statement Item _newItem = new Item(); all the variables in the original item are cleared. Below is a stripped down version of the class. What was removed is the rest of the properties and the methods to interact with the database to make this a smaller post.

I am not sure what I have wrong that is causing this. I am using IDisposable so that I can use the using statement when inserting bulk data.

public class Item : IDisposable 
{
        private static int _itemID = 0;
        private static string _description = null;
        private static string _partNumber = null;
		
		public Item(Int32 ID = -1)
        {
            ClearVariables();
            if (ID >0)
            {
                SelectItem(ID);
            }
        }

        public void Dispose() 
        {
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
                return;

            if (disposing)
            {
                // Free any other managed objects here.
                //
            }
            disposed = true;
        }


        public int ItemID { get { return _itemID; } }

        public string Description { 
            get { return _description; } 
            set { _description = value; } 
        }

        public string PartNumber { 
            get { return _partNumber; } 
            set { _partNumber = value; } 
        }
}
Developer technologies C#
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-02-11T17:12:56.5233333+00:00

    As suggested, because the backing data for properties is static, all instances of the class have the same values. Change a value in one, it’s changed in all.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-02-11T02:03:58.4666667+00:00

    Perhaps the following might work for you.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void RunSampleButton_Click(object sender, EventArgs e)
        {
            Person person = new Person() {FirstName = "Karen", LastName = "Payne"};
            Person copy = Person.DeepCopy(person);
            MessageBox.Show(copy.ToString());
        }
    }
    
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        
        public static Person DeepCopy(Person person) 
            => new () {FirstName = person.FirstName, LastName = person.LastName};
    
        public override string ToString() => $"{FirstName} {LastName}";
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.