readonly wrapper for a collection

  • Thread starter Thread starter Ashish
  • Start date Start date
A

Ashish

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.Readonly 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
 
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
 
Create your strongly typed collection inheriting from
System.Collections.ReadOnlyCollectionBase.

--

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:
http://www.mztools.com
 
:)

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
 
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.OrderItems.add(123)

and ideally i would like to throw exception when a user tries to do
'myOrder.OrderItems.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
 
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 OrderItemCollection = myOrder.Items


but when i call

myOrder.Items.Add(new object)

it should throw error, saying that this collection is readonly
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top