0

I'm doing a POS software, the client requires the POS to do Cuotes but in separate window, so I dit the main POS window, this have an Audit object wish stores the information of the actual sale history before the cashier cut so I pass the ref Audit object to the frmCuotes so both of the windows are changing the Audit

class POS:Form
{
   EFContext db = new EFContext();
   //Get the last audit open if doesn't exist make a new one
   Audit _audit = getLastAudit(ref db);

   private void POS_Load(object sender, EventArg e)
   {
      frmCuotes cuoteswdw = new frmCuotes(ref _audit, ref db);
      cuoteswdw.Show();
   }

   private void CashierCut()
   {
      _audit.State = Close;
      db.SaveChanges();

      _audit = getLastAudit(ref db);
   }

   //Other more POS operations
}

class frmCuotes:Form
{
   Audit _audit;
   EFContext db;

   public frmCuotes(ref Audit aud, ref EFContext db)
   {
     this._audit = aud;
     this.db = db;
   }

    private void cmdSaveCuote_Click(object sender, EventArgs e)
    {
        var cuote = new Cuote();
        //fill Cuote object ...

        _audit.Cuotes.Add(cuote)
        db.SaveChanges();
    }
}

class Audit
{
    public int Id { get; set; }
    public int Id_Company { get; set; }
    public int Id_Terminal { get; set; }

    public virtual ICollection<Cuotes> Cuotes { get; set; }
    public virtual ICollection<Sales> Sales { get; set; }
}

So this works fine when frmCuotes adds new Cuotes, the _audit in POS get the changes reflected, but when I change _audit from POS it self i get no changes reflected in frmCuotes, for example when CashierCut() is executed, new Audit is created with different Id in consecuence, but after POS makes this Cashier cut I mention the frmCuotes still have the old Audit.Id. Why is the changes reflected in just one way?

Cesar Romeroo
  • 183
  • 1
  • 1
  • 12
  • You should [*read the documentation* about reference parameters](https://msdn.microsoft.com/en-us/library/14akc2c7(v=vs.90).aspx). Don't make up random "ideas" and hope they'll possibly be true. Also read up about [reference vs value types](https://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c). You need to accept that there are some ideas here that you don't actually understand yet. It's not terribly difficult, but you do need to learn them. The words are being used to signify something you don't understand. It will require some effort – 15ee8f99-57ff-4f92-890c-b56153 Jun 22 '17 at 17:24
  • Possible duplicate of [What is the difference between a reference type and value type in c#?](https://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c) – 15ee8f99-57ff-4f92-890c-b56153 Jun 22 '17 at 17:26
  • Yes, I actually read this http://jonskeet.uk/csharp/parameters.html and another answers in this platform, incluiding the one you referenced, but they explain how you alter a referenced field in a functions and differences between ref and value parameter, and I understand in *that* way, and the way the documentation does. And I understand if I change Audit in frmCuotes it will change in the POS too, beacause it actually works, what I don't specifically know, and what I'm asking help to understand, is why when I assign a new value to the field in POS, the value in frmCuotes isn't changing – Cesar Romeroo Jun 22 '17 at 17:52
  • 1. Please add definition of `Audit` type, in case somebody comes along who wants to try to help. 2. `new frmCuotes(ref Audit, ref db)` -- `Audit` is not the same identifier as `_audit`. What, exactly, are you passing to that constructor? They'll need to know that too. – 15ee8f99-57ff-4f92-890c-b56153 Jun 22 '17 at 17:57
  • You're right I miss that, and thanks for you suggestions, I will try to clarify so people can clearly understand what I'm asking. – Cesar Romeroo Jun 22 '17 at 18:13
  • 1
    Shortly: `ref`s cannot be stored. Create a class with public `db` and `audit` fields/properties. Create, store and use instance of that class in `POS`. Pass, store and use that instance in `frmCuotes`. Anytime you change `audit` field/property in one place, it will change in other place (since they are sharing the same instance holding the field/property). – Ivan Stoev Jun 22 '17 at 18:28
  • 1
    Thanks! that's a very useful comment, I will try that solution and I let you know – Cesar Romeroo Jun 22 '17 at 18:48
  • @EdPlunkett Could you be so kind on remove the duplicate flag, as the answers on the linked post aren't the same to this question? – Cesar Romeroo Jun 22 '17 at 20:26

0 Answers0