0

Apologies for the bad title, hard to sum up.

So what is happening is I am have a form that will load data from the database:

JobModel jobModel = Data.GetJobList(model)[0];

JobModel contains a field for a list of "parts" also known as a "PartModel"

public class JobModel
{
...        
public List<PartModel> parts { get; set; }
...
}

So when a user loads up the form I save the data before they begin data entry by assigning this global JobModel to refer back to in later segments of the code. Also the partsModel is located here as well

public static JobModel previousJobModel = new JobModel();

public static List<PartModel> partModels = new List<PartModel>();

public void LoadFormData(int JobID)
{
...
JobModel jobModel = Data.GetJobList(model)[0];
partModels = jobModel.parts;

previousJobModel = jobModel;
...
}

Now what happens is that during a segment of code, the previousJobModel.parts becomes overwritten when the DELETE section of code is executed

    private void olvJobPartList_RightClick(object sender, BrightIdeasSoftware.CellRightClickEventArgs e)
    {
        PartModel model = (PartModel)e.Model;
        if (model != null)
        {
            selectedModel = model;
            menuStripOLV.Show(Cursor.Position);
        }


    }
private void deletePartToolStripMenuItem_Click(object sender, EventArgs e)
{
    //previousJobModel.parts Count = 7
    var itemToRemove = partModels.Single(r => r.PartNumber == selectedModel.PartNumber && r.partID == 
    selectedModel.partID);
    partModels.Remove(itemToRemove);
    //previousJobModel.parts Count = 6
    populateOLV();

}

Few notes: I did put a couple breakpoints in the "delete" function, before the removal of the part from the list, previousJobModel is normal, after it gets screwed up. I am also getting back into the swing of things with coding in general so I may be missing something dumb here. Also changing other fields within the job causes no issues to the previous job model, only deleting a part from the list

jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51
bill5967
  • 55
  • 2
  • 8
  • 2
    Not 100% sure I understand your issue, but `previousJobModel = jobModel;` share a reference to each other. You would need to clone the jobModel instead of referencing it. See [Deep cloning objects](https://stackoverflow.com/q/78536/719186) – LarsTech Feb 07 '20 at 19:53
  • @LarsTech that would make sense with a reference being shared. Will look into object cloning and let you know if that solves it. Thanks! – bill5967 Feb 07 '20 at 19:56
  • Or if you just care about the part list, try `List previousParts = jobModel.parts.ToArray();` – LarsTech Feb 07 '20 at 19:59
  • I got it worked out. Thanks for @LarsTech comment I did some digging into cloning (which I didn't know was a thing) and found this code located in my JobModel class to fix the issue public JobModel DeepCopy() { JobModel clone = (JobModel)this.MemberwiseClone(); clone.parts = new List(parts); return clone; } – bill5967 Feb 07 '20 at 20:17

0 Answers0