readonly wrapper for a collection

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

    #1

    readonly wrapper for a collection

    hi All,
    I want to create a readonly wrapper for my strongly typed collection, so
    that when i expose it from my containing object as readonly, other
    objects are not able to modify it

    much like Arraylist.Reado nly but strongly typed..

    can i inherit some class in .net or would i have to create two
    collection classes of the same return Type ?

    comments ?, suggestions ?

    regards
    -ashish

  • Cor Ligthert [MVP]

    #2
    Re: readonly wrapper for a collection

    Ashish,

    Can you rephrase your question and than with less words as 'strongly typed'
    , 'wrapper' etc.?

    A returned property from an instanced object, which is set in the class of
    that readonly, is forever read only (if it is not overloaded).

    Cor


    Comment

    • Carlos J. Quintero [VB MVP]

      #3
      Re: readonly wrapper for a collection

      Create your strongly typed collection inheriting from
      System.Collecti ons.ReadOnlyCol lectionBase.

      --

      Best regards,

      Carlos J. Quintero

      MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
      You can code, design and document much faster.
      Free resources for add-in developers:
      MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.


      "Ashish" <asharma@thisis junk.com> escribió en el mensaje
      news:Oe7MEAgzFH A.268@TK2MSFTNG P09.phx.gbl...[color=blue]
      > hi All,
      > I want to create a readonly wrapper for my strongly typed collection, so
      > that when i expose it from my containing object as readonly, other objects
      > are not able to modify it
      >
      > much like Arraylist.Reado nly but strongly typed..
      >
      > can i inherit some class in .net or would i have to create two collection
      > classes of the same return Type ?
      >
      > comments ?, suggestions ?
      >
      > regards
      > -ashish
      >[/color]


      Comment

      • Ashish

        #4
        Re: readonly wrapper for a collection

        :)

        for example i have my Order class that class contains OrderItems
        Collection (strongly typed collection of OrderItems).

        Orders Class exposes a readonly property called Items, Ideally what i
        would like to add/remove order items through Orders.AddItem method and
        not through Items.Add method, but i still want the order class to be
        able to add items in the orderitems collection.

        this can be a little confusing, but please let me know and i cal explain
        with some code samples.

        regards
        -ashish





        Cor Ligthert [MVP] wrote:[color=blue]
        > Ashish,
        >
        > Can you rephrase your question and than with less words as 'strongly typed'
        > , 'wrapper' etc.?
        >
        > A returned property from an instanced object, which is set in the class of
        > that readonly, is forever read only (if it is not overloaded).
        >
        > Cor
        >
        >[/color]

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: readonly wrapper for a collection

          Ashish,

          I have the idea you mean this, I made a simple sample for you.



          You can try it and tell us if it is what you mean

          Cor


          Comment

          • Ashish

            #6
            Re: readonly wrapper for a collection

            Hi Cor,

            thanks for the effort, i saw the code and what i want to do is add items
            through order object. in other words do this

            myOrder.AddItem (123)

            instead of

            myOrder.OrderIt ems.add(123)

            and ideally i would like to throw exception when a user tries to do
            'myOrder.OrderI tems.add(123)', is there any way to expose this
            collection as a true readonly ? or would i have to create and maintain 2
            collections

            regards
            -ashish










            Cor Ligthert [MVP] wrote:[color=blue]
            > Ashish,
            >
            > I have the idea you mean this, I made a simple sample for you.
            >
            > http://www.windowsformsdatagridhelp....1-aabd33779b09
            >
            > You can try it and tell us if it is what you mean
            >
            > Cor
            >
            >[/color]

            Comment

            • Cor Ligthert [MVP]

              #7
              Re: readonly wrapper for a collection

              Ashish,

              And how do you want to get those orderitems?

              Cor


              Comment

              • Ashish

                #8
                Re: readonly wrapper for a collection

                well the order class would maintain and expose those orderitems through
                the readonly items property

                like

                Dim myOrder as new Order(1002)

                dim myItems as OrderItemCollec tion = myOrder.Items


                but when i call

                myOrder.Items.A dd(new object)

                it should throw error, saying that this collection is readonly



                Cor Ligthert [MVP] wrote:[color=blue]
                > Ashish,
                >
                > And how do you want to get those orderitems?
                >
                > Cor
                >
                >[/color]

                Comment

                Working...