Reputation: 1715
If you have a simple data model instance.
class AppleModel
{
int PipCount = 3;
Boolean isFresh = true;
}
And you want to make a Visual Control of this:
class AppleView : PictureBox
{
private AppleModel _model;
public AppleView( AppleModel model )
{
this._model = model;
.........
}
}
Is there a mechanism that allows AppleView to gain access to the properties of the AppleData?
For example:
AppleModel Model = new AppleModel();
AppleView View = new AppleView(Model);
View.PipCount = 99;
//not View.Model.PipCount = 99;
Console.WriteLine(Model.PipCount.ToString()); //99
Upvotes: 0
Views: 33
Reputation: 23793
Is there a mechanism that allows AppleView to gain access to the properties of the AppleData ?
Yes, those are called Properties in C#, an object can easily forward properties to one of its member by redeclaring them:
class AppleModel
{
int PipCount { get; set; } // Auto Property
Boolean isFresh {get ; set; } // Auto Property
}
class AppleView : PictureBox
{
private AppleModel _model;
public AppleView( AppleModel model )
{
this._model = model;
.........
}
int PipCount
{
get { return this._model.PipCount; }
set { this._model.PipCount = value; }
}
int isFresh
{
get { return this._model.PipCount; }
set { this._model.PipCount = value; }
}
}
Upvotes: 1