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. Exposing List<> outside class [modified]

Exposing List<> outside class [modified]

Scheduled Pinned Locked Moved C#
questionsalestutorial
11 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.
  • J Offline
    J Offline
    Jan Sommer
    wrote on last edited by
    #1

    Say i had this class:

    public sealed class Partner
    {
    string something;
    string somethingelse;
    List<Customer> customers = new List<Customer>();

    public List<Customer> Customers
    {
        get { return customers; }
    }
    

    }

    Is this bad practice? If so, how can i make it right and still be able to get all the methods a List<> provides? I know i could use class Partner : List<Customer> but inside Customer there's two Lists so that solution won't work there. Is it really necessary that i write a custom List for each object, just to maintain good coding practice? I'm asking because my teacher says that the above example is bad OOP - and because i'm developing an application that really needs the methods a List<> provides....

    modified on Wednesday, November 26, 2008 8:22 AM

    L T 3 Replies Last reply
    0
    • J Jan Sommer

      Say i had this class:

      public sealed class Partner
      {
      string something;
      string somethingelse;
      List<Customer> customers = new List<Customer>();

      public List<Customer> Customers
      {
          get { return customers; }
      }
      

      }

      Is this bad practice? If so, how can i make it right and still be able to get all the methods a List<> provides? I know i could use class Partner : List<Customer> but inside Customer there's two Lists so that solution won't work there. Is it really necessary that i write a custom List for each object, just to maintain good coding practice? I'm asking because my teacher says that the above example is bad OOP - and because i'm developing an application that really needs the methods a List<> provides....

      modified on Wednesday, November 26, 2008 8:22 AM

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      I think your generics got swallowed by the HTML monster! :)

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 beta 1 - out now!
      ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

      J 1 Reply Last reply
      0
      • L leppie

        I think your generics got swallowed by the HTML monster! :)

        xacc.ide - now with TabsToSpaces support
        IronScheme - 1.0 beta 1 - out now!
        ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

        J Offline
        J Offline
        Jan Sommer
        wrote on last edited by
        #3

        You're right - should be working now.

        1 Reply Last reply
        0
        • J Jan Sommer

          Say i had this class:

          public sealed class Partner
          {
          string something;
          string somethingelse;
          List<Customer> customers = new List<Customer>();

          public List<Customer> Customers
          {
              get { return customers; }
          }
          

          }

          Is this bad practice? If so, how can i make it right and still be able to get all the methods a List<> provides? I know i could use class Partner : List<Customer> but inside Customer there's two Lists so that solution won't work there. Is it really necessary that i write a custom List for each object, just to maintain good coding practice? I'm asking because my teacher says that the above example is bad OOP - and because i'm developing an application that really needs the methods a List<> provides....

          modified on Wednesday, November 26, 2008 8:22 AM

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          Depending on the usage, Customers could be IEnumerable<Customer>. But I do not see any other way of mutating that list.

          Jan Sommer wrote:

          I'm asking because my teacher says that the above example is bad OOP

          So did he not bother to explain to you why he feels that way? Sounds like a bad teacher to me. Ask him if he is happy with what he does ;P

          xacc.ide - now with TabsToSpaces support
          IronScheme - 1.0 beta 1 - out now!
          ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

          J 1 Reply Last reply
          0
          • L leppie

            Depending on the usage, Customers could be IEnumerable<Customer>. But I do not see any other way of mutating that list.

            Jan Sommer wrote:

            I'm asking because my teacher says that the above example is bad OOP

            So did he not bother to explain to you why he feels that way? Sounds like a bad teacher to me. Ask him if he is happy with what he does ;P

            xacc.ide - now with TabsToSpaces support
            IronScheme - 1.0 beta 1 - out now!
            ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

            J Offline
            J Offline
            Jan Sommer
            wrote on last edited by
            #5

            He said he didn't know a better way :P The first solution i came up with was to add this method to class Partner:

            public IEnumerator<Customer> GetCustomersEnumerator()
            {
            return this.cust.GetEnumerator();
            }

            which is working just fine for retrieving all the customers with this code:

            IEnumerator<Customer> alist = partners.GetCustomerEnumerator();

            while(alist.MoveNext())
            {
            Console.WriteLine(alist.Current.someProperty);
            }

            But in the userinterface, when a user clicks the customer (shown in a listview) i'd like to use the customer-index to retrieve the correct customer and send him to another dialog. Currently i'm only able to access each customers information and i can't pick a specific customer, since i don't have access to the list. Am i doing it all wrong? I was considering using a database which would make everything alot easier, but that would be too hard for the other students (according to my teacher :P)

            L 1 Reply Last reply
            0
            • J Jan Sommer

              He said he didn't know a better way :P The first solution i came up with was to add this method to class Partner:

              public IEnumerator<Customer> GetCustomersEnumerator()
              {
              return this.cust.GetEnumerator();
              }

              which is working just fine for retrieving all the customers with this code:

              IEnumerator<Customer> alist = partners.GetCustomerEnumerator();

              while(alist.MoveNext())
              {
              Console.WriteLine(alist.Current.someProperty);
              }

              But in the userinterface, when a user clicks the customer (shown in a listview) i'd like to use the customer-index to retrieve the correct customer and send him to another dialog. Currently i'm only able to access each customers information and i can't pick a specific customer, since i don't have access to the list. Am i doing it all wrong? I was considering using a database which would make everything alot easier, but that would be too hard for the other students (according to my teacher :P)

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              Jan Sommer wrote:

              Am i doing it all wrong?

              You have 2 choices: 1. Expose it as an IList<Customer> and do cust.ToArray(). 2. Wrap cust in a ReadOnlyCollection<Customer>. I would prefer the latter.

              xacc.ide - now with TabsToSpaces support
              IronScheme - 1.0 beta 1 - out now!
              ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

              J 1 Reply Last reply
              0
              • J Jan Sommer

                Say i had this class:

                public sealed class Partner
                {
                string something;
                string somethingelse;
                List<Customer> customers = new List<Customer>();

                public List<Customer> Customers
                {
                    get { return customers; }
                }
                

                }

                Is this bad practice? If so, how can i make it right and still be able to get all the methods a List<> provides? I know i could use class Partner : List<Customer> but inside Customer there's two Lists so that solution won't work there. Is it really necessary that i write a custom List for each object, just to maintain good coding practice? I'm asking because my teacher says that the above example is bad OOP - and because i'm developing an application that really needs the methods a List<> provides....

                modified on Wednesday, November 26, 2008 8:22 AM

                T Offline
                T Offline
                Thomas Weller 0
                wrote on last edited by
                #7

                Why for heavens sake should this be bad coding practice?? This is the normal way to do it: Declaring a private field and exposing it through a public property. There's no other way to do it right - your teacher is definitely wrong! There is one concern with this I can think of: 1. The list should be immutable. If so, simply use customers.AsReadOnly() in your get accessor and provide some methods like Add/RemoveCustomer in your Partner class. Regards Thomas

                www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                Programmer - an organism that turns coffee into software.

                A J 2 Replies Last reply
                0
                • L leppie

                  Jan Sommer wrote:

                  Am i doing it all wrong?

                  You have 2 choices: 1. Expose it as an IList<Customer> and do cust.ToArray(). 2. Wrap cust in a ReadOnlyCollection<Customer>. I would prefer the latter.

                  xacc.ide - now with TabsToSpaces support
                  IronScheme - 1.0 beta 1 - out now!
                  ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

                  J Offline
                  J Offline
                  Jan Sommer
                  wrote on last edited by
                  #8

                  Thanks... Didn't know of the ReadOnlyCollection but that seems reasonable to use..

                  1 Reply Last reply
                  0
                  • T Thomas Weller 0

                    Why for heavens sake should this be bad coding practice?? This is the normal way to do it: Declaring a private field and exposing it through a public property. There's no other way to do it right - your teacher is definitely wrong! There is one concern with this I can think of: 1. The list should be immutable. If so, simply use customers.AsReadOnly() in your get accessor and provide some methods like Add/RemoveCustomer in your Partner class. Regards Thomas

                    www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                    Programmer - an organism that turns coffee into software.

                    A Offline
                    A Offline
                    Alan Balkany
                    wrote on last edited by
                    #9

                    I tend to agree. The only design guideline this might violate is from Bill Wagner's "Effective C#, Item 23: "Avoid Returning References to Internal Class Objects". But Thomas' suggestion to return a read-only object addresses (no pun intended) this issue.

                    1 Reply Last reply
                    0
                    • T Thomas Weller 0

                      Why for heavens sake should this be bad coding practice?? This is the normal way to do it: Declaring a private field and exposing it through a public property. There's no other way to do it right - your teacher is definitely wrong! There is one concern with this I can think of: 1. The list should be immutable. If so, simply use customers.AsReadOnly() in your get accessor and provide some methods like Add/RemoveCustomer in your Partner class. Regards Thomas

                      www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                      Programmer - an organism that turns coffee into software.

                      J Offline
                      J Offline
                      Jan Sommer
                      wrote on last edited by
                      #10

                      i'm glad to hear i'm not completely wrong.. this sure have saved me a headache..

                      T 1 Reply Last reply
                      0
                      • J Jan Sommer

                        i'm glad to hear i'm not completely wrong.. this sure have saved me a headache..

                        T Offline
                        T Offline
                        Thomas Weller 0
                        wrote on last edited by
                        #11

                        Always at your service... :) Regards Thomas

                        www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                        Programmer - an organism that turns coffee into software.

                        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