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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to create a colletion of properties in a C# class

How to create a colletion of properties in a C# class

Scheduled Pinned Locked Moved C#
questioncsharptutorial
9 Posts 4 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.
  • B Offline
    B Offline
    bnath001
    wrote on last edited by
    #1

    Hello folks, Happy New Year!!! I have a class that has couple of properties. Can I have a property in that class that can be a collection. Like say the MarketData. can I have a property say "FailedCurves" . It is a string property. But only thing is, this property is an ArrayList or Dictionary object that can have multiple string items in it. How can I do that in C#? Am I thinking correct? Thanks much

    public class MarketData
    {
    #region "Private Data Members"
    private int _countAdvanceCurve;
    private int _countAdvancesSBCAgencyCurve;
    private int _countAdvancesSBCAAACurve;
    private int _countFhlbsfTreasuryCurve;
    private int _countDNCOCurve;
    #endregion "Private Data Members"
    #region "Public Data Members"
    public int CountAdvanceCurve
    {
    get { return _countAdvanceCurve; }
    set { _countAdvanceCurve = value; }
    }
    }

    P A OriginalGriffO 3 Replies Last reply
    0
    • B bnath001

      Hello folks, Happy New Year!!! I have a class that has couple of properties. Can I have a property in that class that can be a collection. Like say the MarketData. can I have a property say "FailedCurves" . It is a string property. But only thing is, this property is an ArrayList or Dictionary object that can have multiple string items in it. How can I do that in C#? Am I thinking correct? Thanks much

      public class MarketData
      {
      #region "Private Data Members"
      private int _countAdvanceCurve;
      private int _countAdvancesSBCAgencyCurve;
      private int _countAdvancesSBCAAACurve;
      private int _countFhlbsfTreasuryCurve;
      private int _countDNCOCurve;
      #endregion "Private Data Members"
      #region "Public Data Members"
      public int CountAdvanceCurve
      {
      get { return _countAdvanceCurve; }
      set { _countAdvanceCurve = value; }
      }
      }

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      You can have a property that is a List or Dictionary, no problem -- but you may want to make it read only. What exactly is confusing you?

      1 Reply Last reply
      0
      • B bnath001

        Hello folks, Happy New Year!!! I have a class that has couple of properties. Can I have a property in that class that can be a collection. Like say the MarketData. can I have a property say "FailedCurves" . It is a string property. But only thing is, this property is an ArrayList or Dictionary object that can have multiple string items in it. How can I do that in C#? Am I thinking correct? Thanks much

        public class MarketData
        {
        #region "Private Data Members"
        private int _countAdvanceCurve;
        private int _countAdvancesSBCAgencyCurve;
        private int _countAdvancesSBCAAACurve;
        private int _countFhlbsfTreasuryCurve;
        private int _countDNCOCurve;
        #endregion "Private Data Members"
        #region "Public Data Members"
        public int CountAdvanceCurve
        {
        get { return _countAdvanceCurve; }
        set { _countAdvanceCurve = value; }
        }
        }

        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #3

        Yes you can have any sorts of collection in a properyt.

        public ObservableCollection CountAdvanceCurve
        {
        get { return _countAdvanceCurve; }
        set { _countAdvanceCurve = value; }
        }

        or

        public Dictionary CountAdvanceCurve
        {
        get { return _countAdvanceCurve; }
        set { _countAdvanceCurve = value; }
        }

        and so on.

        WP Apps - Color Search | Arctic | XKCD | Sound Meter | Speed Dial

        1 Reply Last reply
        0
        • B bnath001

          Hello folks, Happy New Year!!! I have a class that has couple of properties. Can I have a property in that class that can be a collection. Like say the MarketData. can I have a property say "FailedCurves" . It is a string property. But only thing is, this property is an ArrayList or Dictionary object that can have multiple string items in it. How can I do that in C#? Am I thinking correct? Thanks much

          public class MarketData
          {
          #region "Private Data Members"
          private int _countAdvanceCurve;
          private int _countAdvancesSBCAgencyCurve;
          private int _countAdvancesSBCAAACurve;
          private int _countFhlbsfTreasuryCurve;
          private int _countDNCOCurve;
          #endregion "Private Data Members"
          #region "Public Data Members"
          public int CountAdvanceCurve
          {
          get { return _countAdvanceCurve; }
          set { _countAdvanceCurve = value; }
          }
          }

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Yes you can. When you create a property, you can return anything you want - including objects you create as required. For example, if you want to return only those values which are below 1000:

          public Dictionary<string, int> LowValues
          {
          get
          {
          Dictionary<string, int> dict = new Dictionary<string, int>();
          if (_countAdvanceCurve < 1000) dict.Add("AdvanceCurve", _countAdvanceCurve);
          if (_countAdvancesSBCAgencyCurve < 1000) dict.Add("AdvancesSBCAgencyCurve", _countAdvancesSBCAgencyCurve);
          if (_countAdvancesSBCAAACurve < 1000) dict.Add("AdvancesSBCAAACurve", _countAdvancesSBCAAACurve);
          if (_countFhlbsfTreasuryCurve < 1000) dict.Add("FhlbsfTreasuryCurve", _countFhlbsfTreasuryCurve);
          if (_countDNCOCurve < 1000) dict.Add("DNCOCurve", _countDNCOCurve);
          return dict;
          }
          }

          This is one of the big advantages of Properties over fields - you do not have to store anything in a way that makes sense to the outside world. You can store in the best way for your class, and provide the outside world with the info it want, how it wants it, only when it asks for it.

          If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          B 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Yes you can. When you create a property, you can return anything you want - including objects you create as required. For example, if you want to return only those values which are below 1000:

            public Dictionary<string, int> LowValues
            {
            get
            {
            Dictionary<string, int> dict = new Dictionary<string, int>();
            if (_countAdvanceCurve < 1000) dict.Add("AdvanceCurve", _countAdvanceCurve);
            if (_countAdvancesSBCAgencyCurve < 1000) dict.Add("AdvancesSBCAgencyCurve", _countAdvancesSBCAgencyCurve);
            if (_countAdvancesSBCAAACurve < 1000) dict.Add("AdvancesSBCAAACurve", _countAdvancesSBCAAACurve);
            if (_countFhlbsfTreasuryCurve < 1000) dict.Add("FhlbsfTreasuryCurve", _countFhlbsfTreasuryCurve);
            if (_countDNCOCurve < 1000) dict.Add("DNCOCurve", _countDNCOCurve);
            return dict;
            }
            }

            This is one of the big advantages of Properties over fields - you do not have to store anything in a way that makes sense to the outside world. You can store in the best way for your class, and provide the outside world with the info it want, how it wants it, only when it asks for it.

            If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

            B Offline
            B Offline
            bnath001
            wrote on last edited by
            #5

            Thanks much. Best forum ever. I don't think I ever posted here... This is the finished code.

            private List _curveswithnodata = new List();

                public List Curveswithnodata
                {
                    get { return \_curveswithnodata; }
                    set { \_curveswithnodata = value; }
                }
            

            public method() { if (basedonsomecondition) { this.Curveswithnodata.Add(CurveName); } }

            OriginalGriffO 1 Reply Last reply
            0
            • B bnath001

              Thanks much. Best forum ever. I don't think I ever posted here... This is the finished code.

              private List _curveswithnodata = new List();

                  public List Curveswithnodata
                  {
                      get { return \_curveswithnodata; }
                      set { \_curveswithnodata = value; }
                  }
              

              public method() { if (basedonsomecondition) { this.Curveswithnodata.Add(CurveName); } }

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              It's a good idea to create a new List if you are going to return it to the outside world - remember that if you return a List, you actually return a reference to the list, so any changes made to it outside the class affect the version the class is using, but without you being notified. If you return a copy of a list then the outside world can do what it likes with the list without it affecting the one your class is using and potentially buggering up your data! :laugh:

              private List<string> _curveswithnodata = new List<string>();

              public List<string> Curveswithnodata
              {
              get { return new List<string>(_curveswithnodata); }
              set { _curveswithnodata = value; }
              }

              The other solution is to return a read only version:

              get { return \_curveswithnodata.AsReadOnly());
              

              But that returns a different type: ReadOnlyCollection<T> rather than List<T> which generally makes it harder to work with outside the class.

              If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              B 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                It's a good idea to create a new List if you are going to return it to the outside world - remember that if you return a List, you actually return a reference to the list, so any changes made to it outside the class affect the version the class is using, but without you being notified. If you return a copy of a list then the outside world can do what it likes with the list without it affecting the one your class is using and potentially buggering up your data! :laugh:

                private List<string> _curveswithnodata = new List<string>();

                public List<string> Curveswithnodata
                {
                get { return new List<string>(_curveswithnodata); }
                set { _curveswithnodata = value; }
                }

                The other solution is to return a read only version:

                get { return \_curveswithnodata.AsReadOnly());
                

                But that returns a different type: ReadOnlyCollection<T> rather than List<T> which generally makes it harder to work with outside the class.

                If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

                B Offline
                B Offline
                bnath001
                wrote on last edited by
                #7

                I am building a webservice (not WCF). I need to store two values (name,value pairs). Instead of using List, I used Dictionary. But it give me error message saying "Can't serialize the dictionary object. Which collections object I can use (to be able to serialize) to store name value pair in a webservice . Thanks

                OriginalGriffO 1 Reply Last reply
                0
                • B bnath001

                  I am building a webservice (not WCF). I need to store two values (name,value pairs). Instead of using List, I used Dictionary. But it give me error message saying "Can't serialize the dictionary object. Which collections object I can use (to be able to serialize) to store name value pair in a webservice . Thanks

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  Dictionaries are not serializable via SOAP or XML serializers - but they are via DataContractSerializer[^] The only other option is to use a List<KeyValuePair<string, yourtype>> which (frankly) is a PITA! :laugh: But, a serializable PITA!

                  If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  B 1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Dictionaries are not serializable via SOAP or XML serializers - but they are via DataContractSerializer[^] The only other option is to use a List<KeyValuePair<string, yourtype>> which (frankly) is a PITA! :laugh: But, a serializable PITA!

                    If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.

                    B Offline
                    B Offline
                    bnath001
                    wrote on last edited by
                    #9

                    Griff, Thanks. But you can't use this as a property of class. Can't have getters and setters. nath

                    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