locked
Moving Properties into category RRS feed

  • Question

  • Hi,

    My class has quite a few variables and I have get/set properties for all of them. All these are broadly related so I do not wish to have separate class for them

    Now, I wish to categorize these properties as follows. How do I implement the categorization of properties within my class ?

    1myclass.category1.property1 = "value"
    2myclass.category2.property1 = "value2"
    Sunday, January 25, 2009 8:09 AM

Answers

  • Make classes for each property category and move your property declarations into them.  Add a "category1" and "category2" property to myclass that returns an instance of that class.  For example:

      public class myclass {
        public class category1properties {
          public string property1 { get; set; }
          public string property2 { get; set; }
        }
        public class category2properties {
          public string property1 { get; set; }
        }
        private category1properties cat1 = new category1properties();
        private category2properties cat2 = new category2properties();

        public category1properties category1 { get { return cat1; } }
        public category2properties category2 { get { return cat2; } }

        public static void test() {
          var mc = new myclass();
          mc.category1.property2 = "value";
          mc.category2.property1 = "value2";
        }
      }


    Hans Passant.
    • Marked as answer by NRworld Monday, January 26, 2009 3:47 AM
    Sunday, January 25, 2009 12:58 PM

All replies

  • Make classes for each property category and move your property declarations into them.  Add a "category1" and "category2" property to myclass that returns an instance of that class.  For example:

      public class myclass {
        public class category1properties {
          public string property1 { get; set; }
          public string property2 { get; set; }
        }
        public class category2properties {
          public string property1 { get; set; }
        }
        private category1properties cat1 = new category1properties();
        private category2properties cat2 = new category2properties();

        public category1properties category1 { get { return cat1; } }
        public category2properties category2 { get { return cat2; } }

        public static void test() {
          var mc = new myclass();
          mc.category1.property2 = "value";
          mc.category2.property1 = "value2";
        }
      }


    Hans Passant.
    • Marked as answer by NRworld Monday, January 26, 2009 3:47 AM
    Sunday, January 25, 2009 12:58 PM
  • Thanks nobugz
    Monday, January 26, 2009 3:47 AM