1

I got some confusion about "the right way" to use a reference in a partial class. Basically i wrote a WPF program which has different Menus. Every Menu got the same Viewmodel and some data-related object class. In my case i call the Object "DataModel" which i want to use as reference in every menu. I just came across a problem when ich switched my DataModel from a static object to the desired instance for every Menu as input ref. (i still want to use one and the same DataModel for every menu though...) But in the "lower" methods it says that _dm is simply not defined.

Code shortly summarized as:

 public partial class FormatWPF : UserControl
    {            
        public FormatWPF(DataModel _dm)
        {
            InitializeComponent();
            if (this.DataContext == null)
            {
                this.DataContext = _dm.g1.MVM;
            }                
        }


        // here come several Methods with which i want to calculate stuff and "manipulate" the DataModel 
        private void Steinformat_berechnen()
        { 
           _dm.g1.FormatNr = _dm.g1.FormatAnzahl + 1;                                     
        }
        //....
    }

Shortly said i want to use the _dm which is given as input ref in the Constructor of the class object for every other method in the whole partial class as well (is it really necessary to define this ref for every method ?) Using the DataModel as static seemed so easy for me.... but basically it is "wrong" ?

Thanks in advance for some help and tips about doing it the right way.

Maybe i was a little bit unclear. The thing is i want to use just one DataModel for all the menu and my whole project. Nevertheless i dont want to make it as static ( there occured some other confusion in later parts of my code... ) So basically i have to give in the DataModel as ref for all the Menus...

Concerning your answer: I know the possibility to define another

private Datamodel _dm; 

in the namespace..

But im not quite sure about:

1)won't i got here some additional "memory" usage by defining another DataModel for every menu ? becuase it is somehow "big"

2)when i now calculate data in the _dm, will it change for the "complete" program ? like in the former static Model ?

I hope to make the DataModel static then is not the "right answer" to my problem because i just wanted to get away from this somehow ... hm

Best regards Knally

Knally
  • 31
  • 3

1 Answers1

3

Yes, static is very wrong if the DataModel is a per-instance thing (static means all the instances would be using the same value); but it can still be an instance field:

private DataModel _dm;
public FormatWPF(DataModel dataModel)
{
    _dm = dataModel;
    // the rest of your constructor code here
}

Now you can use _dm in all of your other instance methods, and everything should be fine. If you only ever need _dm.g1, you could perhaps store that value as the field, instead of the model itself.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Will it change the data of my "whole" DataModel ? when i do it this way ? Basically i just have one and the same DataModel for all my menus. Thanks! – Knally Oct 06 '22 at 08:53
  • @Knally if `DataModel` is a `class`, then: you're always just storing the reference; no matter how many fields and locals you assign with that value: there's still only one object, and all of them just point to *the same object*. A reference is an address - think about it in the real world sense; if you have 200 pieces of paper with an address on, and an instruction is "go to this address and paint the door red", then anyone else with that same address on *their* bit of paper, when going to that address, later should see a red door. There is *only one door*, no matter how many pieces of paper. – Marc Gravell Oct 06 '22 at 09:12
  • Ok perfect ! I think i understand it now much better. For a good "feeling" let me just know: If i declare another private DataModel _dm; And then set it to my input ref _dm = dm; Will i have additional "memory" usage for this. Because in my understanding ... yeah i just declared another big object.. and i do it for like 8 different menus ...hmm – Knally Oct 06 '22 at 09:14
  • @Knally if we assume `DataModel` is a `class` (this detail matters), then: you're not declaring/creating objects here - you're just copying the bit of paper with the address on it (in reality: 4 bytes or 8 bytes, depending on 32-bit or 64-bit); the only time you'd be creating a new object would be something like `new DataModel(...)` – Marc Gravell Oct 06 '22 at 09:28