-1

At run time my program assign 2 properties with same collection. I took two properties so that if changing to collection will be done by one property and second will hold the same collection as it is. But behind the scene both are pointing to same, i can not hold the collection with no changes to one property. I require this to wok this ok and cancel button so that if no changes one prperty will take care or this, and if changes done the other property wil take care of this.

How can i manage this?

like this

private void btnOK_Click(object sender, EventArgs e)
        {
            Program.currOrder.OrderItems[Program.editIndex].AppliedCustomization = lstBtn;//objFreecusatomization.AllAppliedItems;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Program.currOrder.OrderItems[Program.editIndex].AppliedCustomization = actualBtnLIst;
            this.DialogResult = DialogResult.Cancel;
        }

these are the 2 properties which are being assined from some other program

public List<btnObject> lstBtn;
        public List<btnObject> actualBtnLIst { get; set; }

from other program this is how it is set

  frmPrepare.actualBtnLIst = frmPrepare.lstBtn = Program.currOrder.OrderItems[currIdx].AppliedCustomization;
NoviceToDotNet
  • 10,387
  • 36
  • 112
  • 166
  • Any references or idea atleast, before giving vote for close. I am unable to manage it, and feel ti is a legitimate question. – NoviceToDotNet Aug 28 '13 at 12:47

2 Answers2

1

It's hard to tell what you're asking, but if you're trying to keep a copy of the original list you will need to make a copy instead of assigning the reference.

frmPrepare.lstBtn = Program.currOrder.OrderItems[currIdx].AppliedCustomization;
frmPrepare.actualBtnLIst = frmPrepare.lstBtn.ToList();
// ToList will create a copy of each item reference in the collection.
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
  • so my statement is different from this. that in single line, please elaborate a little. – NoviceToDotNet Aug 28 '13 at 12:50
  • If you're trying to keep track of the changes to each object then you will need to either create deep copies or implement the change tracking on the objects themselves [What would be the best way to implement change tracking on an object](http://stackoverflow.com/questions/2363801/what-would-be-the-best-way-to-implement-change-tracking-on-an-object) – Dustin Kingen Aug 28 '13 at 12:59
1

There are tow options either when creating actual list, get all the items from the original list and fill it up into the new list, Suppose in a for loop(Transferring the value of all the properties one by one), you can also use reflection for the same.

Other is if the object is serialize then first serialize the original list and then deserialize it back so that the it is copies by value and not by reference.

For the second option code will go something like this:

using (var memoryStream = new MemoryStream())
{
   var binaryFormatter = new BinaryFormatter();
   binaryFormatter.Serialize(memoryStream, <Your Original List Object>);
   memoryStream.Position = 0;

   <You actual List Object> =  binaryFormatter.Deserialize(memoryStream);
}
Abhishek Jain
  • 2,597
  • 1
  • 18
  • 12