Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to use class property

How to use class property

Scheduled Pinned Locked Moved C#
tutorialquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    robert110
    wrote on last edited by
    #1

    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.

    L S 2 Replies Last reply
    0
    • R robert110

      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.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • R robert110

        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.

        S Offline
        S Offline
        Sreenath Madyastha
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups