473,385 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.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

Nov 21 '05 #1
7 1474
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
Nov 21 '05 #2
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

"Ashish" <as*****@thisisjunk.com> escribió en el mensaje
news:Oe*************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #3
:)

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:
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

Nov 21 '05 #4
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
Nov 21 '05 #5
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




Cor Ligthert [MVP] wrote:
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

Nov 21 '05 #6
Ashish,

And how do you want to get those orderitems?

Cor
Nov 21 '05 #7
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

Cor Ligthert [MVP] wrote:
Ashish,

And how do you want to get those orderitems?

Cor

Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: James S | last post by:
Hi, Basically I've been fighting with this code for a few days now and can't seem to work around this problem. Included is the output, the program I use to get this error and the source code for...
1
by: Alvin Bruney | last post by:
I posted this a few posts ago and nobody responded, probably because nobody gives a rat's ass. But i'd like to suggest that the readonly restriction when iterating a collection be reviewed. I have...
0
by: emma middlebrook | last post by:
Tieing myself up in knots a bit here but am thinking about how to do the following two things ... 1. Unmodifiable ArrayList of elements of type T. If we are OK with checking at run-time we can...
2
by: Alan | last post by:
Hi all, I have one class including one collection. I use propertygrid to display and edit its properties. I'm trying to use reflection to change the status of one element to be readonly, for...
5
by: cody | last post by:
I know I can use ArrayList.ReadOnly() to create a readonly version of it, but how can I achive a similar thing with a generic collection of .NET 2.0?
10
by: GP | last post by:
Is it possible to iterate through all the controls collection and make the textboxes alone as read only.I don't see a readonly property for the Control.Can some one help me in this context? I...
6
by: moi_bobbo | last post by:
I want to override the existing Combobox class and give it a readonly property so that the backcolor of the combobox will be grey with the forecolor being black as is the case for a textbox set to...
8
by: ljlevend2 | last post by:
If a property's type is an array and the property is exposed in a PropertyGrid, then the property can be expanded so that the user can view and edit the array elements. But, I've noticed that if...
3
by: R.A.M. | last post by:
Hello, I am learning C#.NET and I donst understand difference between meaning and usage of "readonly" and "const" keywords. Could you explain me please. Thank you /RAM/
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.