Custom Collection Confusion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • craig

    Custom Collection Confusion

    Assume that you would like to create a custom collection class that is:

    1. Strongly-typed (only holds Customer objects)
    2. Read-only (user cannot add Customer objects)
    3. Able to be bound to a winforms datagrid (must implement the IList
    interface).

    What would be a good way to create this class?

    1. The CollectionBase abstract base class can be used, but it is not
    read-only.
    2. The ReadOnlyCollect ion abstract base class can be used, but it doesn't
    implement IList and therefore can't be bound.
    3. The IList interface includes an "IsReadOnly " property, so why does the
    ReadOnlyCollect ion base class
    not implement the IList interface, with IsReadOnly set to return "true"??
    (Instead, it doesn't implement IList at all).


  • MHelland

    #2
    Re: Custom Collection Confusion

    > What would be a good way to create this class?

    This seems be a very common question and I'm often confused by the varying
    answers posted here.

    I'm wondering if those with an opinion would be willing to post to a
    WikisWeb for future reference?
    Go here: http://dotnet.wikis.com/wc.dll?dotnet~CollectionClasses
    And click "Edit" to modify the contents of the page to add your two cents.

    Just an idea.


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Custom Collection Confusion

      Craig,

      I would ignore the last post. It's just spam for a C# site. The text
      is so generic I could post it in a gardening newsgroup and get the same
      thing. As an example, check out what I posted:



      What I would do is create a class that extends ReadOnlyCollect ionBase,
      and implement IList yourself, throwing InvalidOperatio nExceptions on the
      implementation of Add, Remove, etc, etc that would violate the read-only
      status.

      Then I would create the custom collection as normal, but not include
      type-safe methods for Remove, Add, and the set indexer.

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - nicholas.paldin o@exisconsultin g.com

      "craig" <e@mail.com> wrote in message
      news:OOj0LriRDH A.1556@TK2MSFTN GP10.phx.gbl...[color=blue]
      > What is a WikisWeb??
      >
      > "MHelland" <mhelland@techm ocracy.net> wrote in message
      > news:Opn1PmiRDH A.2232@TK2MSFTN GP11.phx.gbl...[color=green][color=darkred]
      > > > What would be a good way to create this class?[/color]
      > >
      > > This seems be a very common question and I'm often confused by the[/color][/color]
      varying[color=blue][color=green]
      > > answers posted here.
      > >
      > > I'm wondering if those with an opinion would be willing to post to a
      > > WikisWeb for future reference?
      > > Go here: http://dotnet.wikis.com/wc.dll?dotnet~CollectionClasses
      > > And click "Edit" to modify the contents of the page to add your two[/color][/color]
      cents.[color=blue][color=green]
      > >
      > > Just an idea.
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Randy Jean

        #4
        Re: Custom Collection Confusion

        What are you, a newsgroup Nazi? - I just came out here as a result of
        reading your post. Now I am reminded why I hate newsgroups. I get a
        headache trying to find anything of use out here. Wiki's are a much
        better place to share knowledge. Mike's right, you don't know what
        you're talking about.

        Nicholas Paldino [.NET/C# MVP] wrote:[color=blue]
        > Craig,
        >
        > I would ignore the last post. It's just spam for a C# site. The text
        > is so generic I could post it in a gardening newsgroup and get the same
        > thing. As an example, check out what I posted:
        >
        > http://dotnet.wikis.com/wc.dll?DotNe...kYourSpamSucks
        >
        > What I would do is create a class that extends ReadOnlyCollect ionBase,
        > and implement IList yourself, throwing InvalidOperatio nExceptions on the
        > implementation of Add, Remove, etc, etc that would violate the read-only
        > status.
        >
        > Then I would create the custom collection as normal, but not include
        > type-safe methods for Remove, Add, and the set indexer.
        >
        > Hope this helps.
        >
        >[/color]

        Comment

        • Nicholas Paldino [.NET/C# MVP]

          #5
          Re: Custom Collection Confusion

          Mike,

          I just received an email from Steven Black about this, and apparently, I
          might have erred. However, I do have to say that your post had a few items
          in it that led me to respond in that manner:

          You wrote:
          ---[color=blue]
          > What would be a good way to create this class?[/color]

          This seems be a very common question and I'm often confused by the varying
          answers posted here.
          ---

          Your post was initially the only response to the original poster's post
          in this group. I took "here" to mean this group. Based on that, it would
          seem that the responder did not really read the post, or it was automatic.
          On top of that, a search on google.com for this group only turns up 16
          results for ReadOnlyCollect ionBase dating back to July of 2001.

          In retrospect, I feel that I did act in haste, and I apologize for that.
          As I stated in my email to Steven Black, I have no problem with people
          promoting their sites that promote .NET and knowledge in general, but in my
          initial analysis of the response, I felt that the promotion was coming under
          the guise of helping the original poster, which it certainly did not.

          My apologies to anyone that might have been offended. The reply was in
          no way meant to criticize the content or the efficacy of the site, but
          rather, the methods used to promote it. Looking back, I feel that I have
          erred, and I apologize for that.

          --
          - Nicholas Paldino [.NET/C# MVP]
          - nicholas.paldin o@exisconsultin g.com

          "Mike Helland" <mhelland@techm ocracy.net> wrote in message
          news:u9i8pFjRDH A.1924@TK2MSFTN GP12.phx.gbl...[color=blue][color=green]
          > > It's just spam for a C# site.[/color]
          >
          > You don't know what you're talking about.
          >
          >[/color]


          Comment

          • Randy Jean

            #6
            Re: Custom Collection Confusion

            In light of this apology, I hope you'll accept my apology to you and I
            would also like to retract my calling you a newsgroup Nazi. That was
            uncalled for.

            Nicholas Paldino [.NET/C# MVP] wrote:
            [color=blue]
            > Mike,
            >
            > I just received an email from Steven Black about this, and apparently, I
            > might have erred. However, I do have to say that your post had a few items
            > in it that led me to respond in that manner:
            >
            > You wrote:
            > ---
            >[color=green]
            >>What would be a good way to create this class?[/color]
            >
            >
            > This seems be a very common question and I'm often confused by the varying
            > answers posted here.
            > ---
            >
            > Your post was initially the only response to the original poster's post
            > in this group. I took "here" to mean this group. Based on that, it would
            > seem that the responder did not really read the post, or it was automatic.
            > On top of that, a search on google.com for this group only turns up 16
            > results for ReadOnlyCollect ionBase dating back to July of 2001.
            >
            > In retrospect, I feel that I did act in haste, and I apologize for that.
            > As I stated in my email to Steven Black, I have no problem with people
            > promoting their sites that promote .NET and knowledge in general, but in my
            > initial analysis of the response, I felt that the promotion was coming under
            > the guise of helping the original poster, which it certainly did not.
            >
            > My apologies to anyone that might have been offended. The reply was in
            > no way meant to criticize the content or the efficacy of the site, but
            > rather, the methods used to promote it. Looking back, I feel that I have
            > erred, and I apologize for that.
            >[/color]

            Comment

            • Mike Helland

              #7
              Re: Custom Collection Confusion

              > On top of that, a search on google.com for this group only turns up 16[color=blue]
              > results for ReadOnlyCollect ionBase dating back to July of 2001.[/color]

              Yes, well, I too probably posted my message to the wrong thread. A couple
              days ago there was a thread called:

              'Bad design of C# collections framework. Where is the "Set" collection?'

              And similar ones have seemed to appear every couple of days. I was going to
              post the wikis recommendation to Bad Design one, but decided against it.
              When I saw the new collection thread today, I figured I might as well chuck
              it out there, not seeing that the question was specifically about read only
              collections.

              I'd still like to see a central wikis topic on clr collections, if you have
              anything to add, that'd be cool.


              Comment

              • Nicholas Paldino [.NET/C# MVP]

                #8
                Re: Custom Collection Confusion

                Randy,

                Thank you, it is much appreciated, and apology accepted.

                However, I don't know if I can forgive you for the following things:

                - Criticism of the technology used on my personal site.
                - Criticism of where I was brought up.

                Just kidding =)

                --
                - Nicholas Paldino [.NET/C# MVP]
                - nicholas.paldin o@exisconsultin g.com

                "Randy Jean" <nospam-rjean@ogse.com> wrote in message
                news:%23EbMPXjR DHA.2252@TK2MSF TNGP12.phx.gbl. ..[color=blue]
                > In light of this apology, I hope you'll accept my apology to you and I
                > would also like to retract my calling you a newsgroup Nazi. That was
                > uncalled for.
                >
                > Nicholas Paldino [.NET/C# MVP] wrote:
                >[color=green]
                > > Mike,
                > >
                > > I just received an email from Steven Black about this, and[/color][/color]
                apparently, I[color=blue][color=green]
                > > might have erred. However, I do have to say that your post had a few[/color][/color]
                items[color=blue][color=green]
                > > in it that led me to respond in that manner:
                > >
                > > You wrote:
                > > ---
                > >[color=darkred]
                > >>What would be a good way to create this class?[/color]
                > >
                > >
                > > This seems be a very common question and I'm often confused by the[/color][/color]
                varying[color=blue][color=green]
                > > answers posted here.
                > > ---
                > >
                > > Your post was initially the only response to the original poster's[/color][/color]
                post[color=blue][color=green]
                > > in this group. I took "here" to mean this group. Based on that, it[/color][/color]
                would[color=blue][color=green]
                > > seem that the responder did not really read the post, or it was[/color][/color]
                automatic.[color=blue][color=green]
                > > On top of that, a search on google.com for this group only turns up 16
                > > results for ReadOnlyCollect ionBase dating back to July of 2001.
                > >
                > > In retrospect, I feel that I did act in haste, and I apologize for[/color][/color]
                that.[color=blue][color=green]
                > > As I stated in my email to Steven Black, I have no problem with people
                > > promoting their sites that promote .NET and knowledge in general, but in[/color][/color]
                my[color=blue][color=green]
                > > initial analysis of the response, I felt that the promotion was coming[/color][/color]
                under[color=blue][color=green]
                > > the guise of helping the original poster, which it certainly did not.
                > >
                > > My apologies to anyone that might have been offended. The reply was[/color][/color]
                in[color=blue][color=green]
                > > no way meant to criticize the content or the efficacy of the site, but
                > > rather, the methods used to promote it. Looking back, I feel that I[/color][/color]
                have[color=blue][color=green]
                > > erred, and I apologize for that.
                > >[/color]
                >[/color]


                Comment

                • Rakka Rage

                  #9
                  Re: Custom Collection Confusion



                  "Collection Gen is a Custom Tool Add-In to VS.NET 2002 & 2003 to
                  generate type-safe collections. As it turns out, I did almost none of
                  the work. Jon Flanders figured out how to add a custom tool. Shawn Van
                  Ness implemented the template for type-safe collections. I just put it
                  together.

                  CollectionGen is an add-on to generate code for type-safe collections
                  until we have templates in C# (likely) and VB (unlikely). The benefit
                  of a type-safe collection, of course, is that you can use it without
                  having to cast items to and from objects. Also, Shawn has been very
                  careful to implement a collection class that is very efficient for
                  both reference types and value types.

                  Once you've setup it up and defined your collections in a collection
                  definition file in your project, you'll have type-safe collection
                  classes generated as part of your design-process, as shown here:"


                  "craig" <e@mail.com> wrote in message news:<u8CqqgiRD HA.2148@TK2MSFT NGP11.phx.gbl>. ..[color=blue]
                  > Assume that you would like to create a custom collection class that is:
                  >
                  > 1. Strongly-typed (only holds Customer objects)
                  > 2. Read-only (user cannot add Customer objects)
                  > 3. Able to be bound to a winforms datagrid (must implement the IList
                  > interface).
                  >
                  > What would be a good way to create this class?
                  >
                  > 1. The CollectionBase abstract base class can be used, but it is not
                  > read-only.
                  > 2. The ReadOnlyCollect ion abstract base class can be used, but it doesn't
                  > implement IList and therefore can't be bound.
                  > 3. The IList interface includes an "IsReadOnly " property, so why does the
                  > ReadOnlyCollect ion base class
                  > not implement the IList interface, with IsReadOnly set to return "true"??
                  > (Instead, it doesn't implement IList at all).[/color]

                  Comment

                  • bjs10

                    #10
                    Re: Custom Collection Confusion

                    Since CollectionBase implements IList, derive a class from
                    CollectionBase and implement IsReadOnly in that class.

                    "craig" <e@mail.com> wrote in message news:<u8CqqgiRD HA.2148@TK2MSFT NGP11.phx.gbl>. ..[color=blue]
                    > Assume that you would like to create a custom collection class that is:
                    >
                    > 1. Strongly-typed (only holds Customer objects)
                    > 2. Read-only (user cannot add Customer objects)
                    > 3. Able to be bound to a winforms datagrid (must implement the IList
                    > interface).
                    >
                    > What would be a good way to create this class?
                    >
                    > 1. The CollectionBase abstract base class can be used, but it is not
                    > read-only.
                    > 2. The ReadOnlyCollect ion abstract base class can be used, but it doesn't
                    > implement IList and therefore can't be bound.
                    > 3. The IList interface includes an "IsReadOnly " property, so why does the
                    > ReadOnlyCollect ion base class
                    > not implement the IList interface, with IsReadOnly set to return "true"??
                    > (Instead, it doesn't implement IList at all).[/color]

                    Comment

                    Working...