c# - pass data to constructor -


i’m having weird & frustrating problem passing object between of classes. stems fact scripter , not programmer, , bumbling along. i’m sure i’m doing dumb :)

i trying build wizard dialog has multiple pages. using “internationalised wpf wizard” tutorial codeproject starting point, , attempting adapt domain. i’m getting stuck because wizard pages can’t seem refer model.

i have done following:

  • created class model (let call mydata)
  • created base class view models (viewmodelbase)
  • created view model class each of pages, inheriting viewmodelbase (example below welcomepageviewmodel)
  • created ‘controller’ style view model drives wizard. (wizardcontroller)

when wizard launched, wizardcontroller instantiated. wizardcontroller instantiates mydata.then, wizardcontroller instantiates each of view models remaining pages.

the actual gui seems work fine, , can see view models each of pages being loaded correctly. here’s code:

public class mydata {     private string _somestring;     public mydata(string somestring)     {         _somestring = somestring;     } }  public abstract class viewmodelbase : inotifypropertychanged {     bool _iscurrentpage;     readonly mydata _mydata;     public viewmodelbase(mydata mydata)     {         _mydata = mydata;     } }  public class wizardcontroller : inotifypropertychanged {     mydata _mydata;     public wizardcontroller()     {          _mydata = new mydata("the widgets");     } }   public class welcomepageviewmodel : viewmodelbase {     private mydata _mydata;      public welcomepageviewmodel(mydata mydata)         : base(mydata)     {         _mydata = mydata;         // accessing _mydata fails :(         mylogger.writeline("grabbed instance of mydata: " + _mydata.tostring());     } } 

however, code fails when try access mydata welcomepageviewmodel. on mylogger line in welcomepageviewmodel, error “object reference not set instance of object.” thrown.

basically, i’m trying achieve wizardcontroller setting mydata, , each of wizard pages being able access (and manipulate) it. guidance appreciated!

as rob g suggested in comment, you're re-declaring variable _mydata in inherited classes. correct way organize code let _mydata protected property declared on abstract base class, , use property access variable inheriting classes.

public abstract class viewmodelbase : inotifypropertychanged {     bool _iscurrentpage;      protected mydata mydata { get; private set; }      public viewmodelbase(mydata mydata)     {         mydata = mydata;     } }  public class welcomepageviewmodel : viewmodelbase {       public welcomepageviewmodel(mydata mydata)         : base(mydata)     {        // access protected property        mylogger.writeline("grabbed instance of mydata: " + mydata.tostring());     } } 

edit: fixed copy-paste error...


Comments

Popular posts from this blog

java - JavaFX 2 slider labelFormatter not being used -

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -