How to use class property
-
Can someone please explain why one is correct/better or incorrect/worse?
public class Gatorade { public Gatorade(){} private ArrayList _Ingrediants = null; public ArrayList Ingrediants { get { if (_Ingrediants == null) { _Ingrediants = new ArrayList(); } return _Ingrediants; } set { _Ingrediants = value;} } public void IngrediantsList() { /* Should I Do This */ _**Ingrediants = new ArrayList(); this._Ingrediants.Add("Water"); this._Ingrediants.Add("Sucrose Syrup");** /* Or This: */ **this.Ingrediants.Add("Water"); this.Ingrediants.Add("Sucrose Syrup");** } }
If I do this.Ingrediants.Add("Water"); then I dont have to manually instantiate _Ingrediants but this way I am accessing the public property. Please advice. Thanks. -
Can someone please explain why one is correct/better or incorrect/worse?
public class Gatorade { public Gatorade(){} private ArrayList _Ingrediants = null; public ArrayList Ingrediants { get { if (_Ingrediants == null) { _Ingrediants = new ArrayList(); } return _Ingrediants; } set { _Ingrediants = value;} } public void IngrediantsList() { /* Should I Do This */ _**Ingrediants = new ArrayList(); this._Ingrediants.Add("Water"); this._Ingrediants.Add("Sucrose Syrup");** /* Or This: */ **this.Ingrediants.Add("Water"); this.Ingrediants.Add("Sucrose Syrup");** } }
If I do this.Ingrediants.Add("Water"); then I dont have to manually instantiate _Ingrediants but this way I am accessing the public property. Please advice. Thanks.robert110 wrote:
public ArrayList Ingrediants
In most cases you don't whant to do:
public ArrayList Ingrediants
To initialize the object:private ArrayList _Ingrediants = new ArrayList();
To expose contents of the list publicly use IEnumeration or IList or something rather than returning the reference to the list.led mike
-
Can someone please explain why one is correct/better or incorrect/worse?
public class Gatorade { public Gatorade(){} private ArrayList _Ingrediants = null; public ArrayList Ingrediants { get { if (_Ingrediants == null) { _Ingrediants = new ArrayList(); } return _Ingrediants; } set { _Ingrediants = value;} } public void IngrediantsList() { /* Should I Do This */ _**Ingrediants = new ArrayList(); this._Ingrediants.Add("Water"); this._Ingrediants.Add("Sucrose Syrup");** /* Or This: */ **this.Ingrediants.Add("Water"); this.Ingrediants.Add("Sucrose Syrup");** } }
If I do this.Ingrediants.Add("Water"); then I dont have to manually instantiate _Ingrediants but this way I am accessing the public property. Please advice. Thanks.robert110 wrote:
public void IngrediantsList() { /* Should I Do This */ _Ingrediants = new ArrayList(); this._Ingrediants.Add("Water"); this._Ingrediants.Add("Sucrose Syrup"); /* Or This: */ this.Ingrediants.Add("Water"); this.Ingrediants.Add("Sucrose Syrup"); }
I would rather code like this: public Gatorade() { _Ingrediants = new ArrayList(0); } public ArrayList Ingrediants { get { if (_Ingrediants.Count == 0) { this.IngrediantsList(); } return _Ingrediants; } } private void IngrediantsList() { this._Ingrediants.Add("Water"); this._Ingrediants.Add("Sucrose Syrup"); } of course _Ingredients is a private variable. Do not expose method when it is not necessary. Follow good encapsulation.