-1

This is a C# Windows Forms project. I have a grid on a form. To get the data for the grid, I run a SQL procedure and store the results in a class. I want to have a copy of that class so I know what the values were before the user changes them in the grid. So I assign the class to another class. Then I assign the first class as the grid's datasource. However, after the change is made, both the original class and the copy have the same values. How do I prevent this?

Here's is my code:

List<Unreceive> receivedItems = new List<Unreceive>();
List<Unreceive> listItems = mpBLL.GetMPItemsReceived();

receivedItems = listItems;
gcUnreceive.DataSource = listItems;

at this point, let's say receivedItems.quantity and listItems.quantity have a value of 100.

The user changes the data in the grid so the quantity is 50. That triggers this code:

    private void gvUnreceive_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
    {
      DevExpress.XtraGrid.Views.Grid.GridView gridView = (DevExpress.XtraGrid.Views.Grid.GridView) sender;
      int index = gridView.GetDataSourceRowIndex(rowHandle);
      Unreceive selectedItem = listItems[index];
      Unreceive originalItem = receivedItems[index];

      int newQuantity = selectedItem.quantity;
      int originalQuantity = originalItem.quantity;
    }

At this point I want: newQuantity = 50; originalQuantity = 100;

But what I get is: newQuantity = 50; original Quantity = 50;

It's like I passed a variable by reference instead of by value, but I haven't passed it anywhere. How do I fix this so that the receivedItems class isn't effected by what happens to the listItems class or the datagrid?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
boilers222
  • 1,901
  • 7
  • 33
  • 71
  • Take a look at [Reference types](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/reference-types?WT.mc_id=DT-MVP-5003235), [Value types](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/value-types) and [C# Concepts: Value vs Reference Types](http://www.albahari.com/valuevsreftypes.aspx). – Reza Aghaei Jan 20 '20 at 14:18
  • Also search about deep copy and shallow copy, for example take a look at [Difference between Shallow copy and Deep copy](http://net-informations.com/faq/net/shallow-deep-copy.htm) or [Shallow copy and Deep copy in C#](https://www.geeksforgeeks.org/shallow-copy-and-deep-copy-in-c-sharp/) or this [docs example](https://learn.microsoft.com/en-us/dotnet/api/system.object.memberwiseclone?view=netframework-4.8). – Reza Aghaei Jan 20 '20 at 14:30

1 Answers1

0

Yes you are right that it seems "byRef"

receivedItems = listItems; "object = object" would share the Pointer to where the data is not create a new pointer to a new data structure.

You were on the right track in creating a new list initially. List receivedItems = new List();

You need to loop through your original list and create new distinct list items for the copy list - setting each property to the value of the master list. This will give you two lists with separate memory storage.