0

In my application, I'm trying to edit items of an invoice. Due to a complication I elected to remove the original items from the many to many table and re-insert the updated items.

In my code, I have two objects, the original invoice and the updated invoice. Both are identical because they are both from the same ViewState object. When I to edit the updated invoice object, I find out that the original invoice object was updated as well.

Note that the invoice object and the item objects are both serialize capeable so I can use them in the ViewState.

Is my visual studio acting up or is there a logic behind this?

EDIT

        BankCheque originalCheque = (BankCheque)ViewState["OriginalCheque"];
        BankCheque cheque = (BankCheque)ViewState["OriginalCheque"];
        cheque.chequeCode = tb_chequeCode.Text;
        cheque.client = new Client();
        cheque.client.clientID = hidden_clientId.Value;
        cheque.chequeIssueDate = DateTime.Parse(tb_issueDate.Text);
        cheque.chequePaymentDate = DateTime.Parse(tb_paymentDate.Text);
        cheque.chequeAmount = float.Parse(tb_chequeValue.Text);
        if (cb_chequePaid.Checked)
        {
            cheque.paid = 1;
        }
        else
        {
            cheque.paid = 0;
        }

        cheque.invoices = getNewInvoicesList();
        cheque = addPaymentsToCheque(cheque);

The getNewInvoicesList method is

private List<ClientInvoice> getNewInvoicesList()
    {
        DataTable dt = (DataTable)ViewState["invoicesDt"];
        List<ClientInvoice> invoices = new List<ClientInvoice>();
        ClientInvoice invoice;
        foreach (DataRow row in dt.Rows)
        {
            invoice = new ClientInvoice();
            invoice.invoiceID = Convert.ToInt32(row[0].ToString());
            invoices.Add(invoice);
        }
        return invoices;
    }
  • Visual Studio got nothing to do with run time behavior of your application. Also there is no possible way to answer this without seeing any code. – Filburt Sep 27 '17 at 17:25
  • 1
    Maybe you are having trouble with Reference type variables? https://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c – VDWWD Sep 27 '17 at 17:33
  • I added the code, after calling the getNewInovicesList method, the original cheque is changed as well – Ibrahim Alfors Sep 27 '17 at 17:33

1 Answers1

1

Your underlying issue is with this block:

BankCheque originalCheque = (BankCheque)ViewState["OriginalCheque"];
BankCheque cheque = (BankCheque)ViewState["OriginalCheque"];

When you read in your view state, you are creating two references to the same object. No matter which variable you manipulate, both will change because both point to the same underlying object.

In order to do what you're trying to do, you'll need to read the view state in once and then clone the object. In this case, because you want a completely new copy - including all child objects - what you're looking to do is make a "deep copy" or "deep clone" of the original. The answers to this question should help you get started: Deep cloning objects

Once you have your method to clone, you'll want to update the block above to something like:

BankCheque originalCheque = (BankCheque)ViewState["OriginalCheque"];
BankCheque cheque = originalCheque.Clone();

At that point, you'll have two independent objects to manipulate.

Jesse Squire
  • 6,107
  • 1
  • 27
  • 30