Home > Back-end >  Using ref in (whole) partial class
Using ref in (whole) partial class

Time:10-06

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

CodePudding user response:

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.

  • Related