-1

Presenter Here I called a method called "services.UpdateSelectedDeposit(deposit);". As you can see it calls a method in the below services, which calls a method in the repository to set deposit model based on the DepositID.

public void OnDoubleClicked(object sender, EventArgs e)
{

    if(addTipView.DataGridView.CurrentRow.Index != -1)
    {
        deposit.DepositID = Convert.ToInt32(addTipView.DataGridView.CurrentRow.Cells["DepositID"].Value);
        Console.WriteLine(deposit.DepositID);
        services.UpdateSelectedDeposit(deposit);
        Console.WriteLine(deposit.DepositAmount);
        addTipView.TxtTipAmount = deposit.DepositAmount.ToString();
        addTipView.TxtDate = deposit.DepoistDate.ToString();
        addTipView.TxtHoursWorked = deposit.HoursWorked.ToString();

    }
}

Services

public void UpdateSelectedDeposit(Deposit deposit)
{
    repo.GetSelectedDeposit(deposit);
}

Repository

public void GetSelectedDeposit(Deposit deposit)
{
    using (var context = new TipManagerDBEntities())
    {
        deposit = context.Deposits.Where(x => x.DepositID == deposit.DepositID).FirstOrDefault();
        Console.WriteLine(deposit.DepositAmount);
    }
}

When I print the deposit amount in the repository I get the correct value but when I print the deposit amount in the presenter it's not correct. The deposit class I pass as an argument are passed by reference, right? Why is my value not correct in the presenter.

asnyder002
  • 125
  • 8

1 Answers1

3

When passing an object to a method (terms used loosely) you are actually passing a reference , however the important point here is the actual reference to that object is being passed by value. Meaning updating it (overwriting it) does nothing higher up in the call chain .

If you want to overwrite that reference (pass the reference by reference), then you will need to use the ref keyword (where appropriate).

public void GetSelectedDeposit(ref Deposit deposit)
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Okay, so when I alter the object in the method does it create a new object on the heap? Also, just to help me understand, in java, this isn't the case, right? I understand Java would be passing the value of the reference in the parameter but essentially they would be referencing the same object on the heap. So this would work in Java. Is that correct? – asnyder002 Jul 22 '19 at 04:20
  • @asnyder002 Don't worry about stacks and heaps, however, yes you will get a newed up bunch of memory, at a different location in memory, and a different reference address to that memory. I am not sure what java does, however id assume they are pretty similar in the way they work (don't quote me on that). if you are only altering the object at that address, ie, changing a property, then you dont need the `ref` its only when you are trying to assign the reference a new reference which you are in this case – TheGeneral Jul 22 '19 at 04:28
  • Ahhh, your last sentence really made it click for me. Thanks for the help man. – asnyder002 Jul 22 '19 at 04:40