Containing sets of properties in some sort of object -- please help!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • almurph@altavista.com

    #1

    Containing sets of properties in some sort of object -- please help!

    Hi,

    I have an object with 3 properties, 2 int arrays and an int. I need
    another object to contain a set of these objects i.e something to
    consolidate a set of them but able too differenciate between each set.

    For example:

    First Set:

    int[] a
    int[] b
    int c

    Second Set:

    int[] d
    int[] e
    int f


    Third Set:
    etc
    etc
    etc

    My problem is though I don't know how to do this? Is the best type of
    object an indexer? any help/comments/suggestions/code-sampels much
    appreciated.

    Thanks,
    Al.
  • Marc Gravell

    #2
    Re: Containing sets of properties in some sort of object -- pleasehelp!

    If the "2 int arrays and an int" represents something, then I would
    make that a class* by itself, and simply encapsulate a few of them:

    public class Foo {
    int[] a; // with suitable accessors
    int[] b;
    int c;
    }

    class Bar {
    // pick more meaningful names...
    public Foo FirstSet {get;set;}
    public Foo SecondSet {get;set;}
    public Foo ThirdSet {get;set;}
    }

    If the number of Foo instances is indeterminate, then a List<Fooor
    similar would work, and indeed, an indexer may be desirable.

    class Bar {
    public Bar() {Bar = new List<Foo>();}
    public List<FooBar {get;private set;}
    }

    Marc

    *=or /possibly/ a struct - but class should be the default

    Comment

    • Ignacio Machin ( .NET/ C# MVP )

      #3
      Re: Containing sets of properties in some sort of object -- pleasehelp!

      On Sep 22, 12:00 pm, "almu...@altavi sta.com" <almu...@altavi sta.com>
      wrote:
      Hi,
      >
              I have an object with 3 properties, 2 int arrays and an int. I need
      another object to contain a set of these objects i.e something to
      consolidate a set of them but able too differenciate between each set.
      >
              For example:
      >
      First Set:
      >
      int[] a
      int[] b
      int c
      >
      Second Set:
      >
      int[] d
      int[] e
      int f
      >
      Third Set:
      etc
      etc
      etc
      >
              My problem is though I don't know how to do this? Is the best type of
      object an indexer? any help/comments/suggestions/code-sampels much
      appreciated.
      >
      Thanks,
      Al.
      Hi,
      When you say you have an object, are you meaning that you have a class
      with three properties as you describe?
      If not, you should create such a class.

      Then the wrapping class depend of the operations you want to perform
      on the collection of the defined class. If all you do is iterate in
      the collection then a List<MyClassis enough.
      Otherwise you can have a class that has a collection property (it can
      be an indexer) that hold the collection of your defined class.

      Comment

      • almurph@altavista.com

        #4
        Re: Containing sets of properties in some sort of object -- pleasehelp!

        Hi Marc,

        Thanks for the reply - yeah I was trying something like this. Once
        class encapsulating another class with the properties. However I get
        the error message: "member names cannot be the same as their enclosing
        type"
        at:

        public List<FooBar {get;private set;}

        Under the word: Bar

        Any ideas?

        Marc Gravell wrote:
        If the "2 int arrays and an int" represents something, then I would
        make that a class* by itself, and simply encapsulate a few of them:
        >
        public class Foo {
        int[] a; // with suitable accessors
        int[] b;
        int c;
        }
        >
        class Bar {
        // pick more meaningful names...
        public Foo FirstSet {get;set;}
        public Foo SecondSet {get;set;}
        public Foo ThirdSet {get;set;}
        }
        >
        If the number of Foo instances is indeterminate, then a List<Fooor
        similar would work, and indeed, an indexer may be desirable.
        >
        class Bar {
        public Bar() {Bar = new List<Foo>();}
        public List<FooBar {get;private set;}
        }
        >
        Marc
        >
        *=or /possibly/ a struct - but class should be the default

        Comment

        • Marc Gravell

          #5
          Re: Containing sets of properties in some sort of object -- pleasehelp!

          Oops! Rename the list property, for example to Items:
          >class Bar {
          > public Bar() {Items = new List<Foo>();}
          > public List<FooItems {get;private set;}
          >}
          Marc

          Comment

          • almurph@altavista.com

            #6
            Re: Containing sets of properties in some sort of object -- pleasehelp!

            On Sep 23, 1:01 pm, Marc Gravell <marc.grav...@g mail.comwrote:
            Oops! Rename the list property, for example to Items:
            >
            class Bar {
              public Bar() {Items = new List<Foo>();}
              public List<FooItems {get;private set;}
            }
            >
            Marc
            Marc,

            Thanks. Working like a charm now. I think I'll modify it to an
            indexer, may be slightly more readable.
            Thanks again,
            Al.

            Comment

            Working...