ArrayList with restricted access
-
I have a class with a function that returns an ArrayList. The problem is that I don't want any client that calls the function to be able to add or delete items from the ArrayList. I want to force the client to use my class' other functions to make any modifications (because related data has to be updated). Unfortunately, speed is an issue, so I can't just copy the ArrayList and send the copy. Also, a copy would allow the client to think he is making a change when it is actually being ignored. I'd prefer him to get a compile error if he calls Add(), for instance. I can't find any C# syntax that can make a returned value (or a returned parameter) read only. (The C# "readonly" syntax won't let the client replace the ArrayList with another, but WILL let the client change the content of the ArrayList, and so it is insufficient). Any ideas would be appreciated. Mark 11 Feb 2007
-
I have a class with a function that returns an ArrayList. The problem is that I don't want any client that calls the function to be able to add or delete items from the ArrayList. I want to force the client to use my class' other functions to make any modifications (because related data has to be updated). Unfortunately, speed is an issue, so I can't just copy the ArrayList and send the copy. Also, a copy would allow the client to think he is making a change when it is actually being ignored. I'd prefer him to get a compile error if he calls Add(), for instance. I can't find any C# syntax that can make a returned value (or a returned parameter) read only. (The C# "readonly" syntax won't let the client replace the ArrayList with another, but WILL let the client change the content of the ArrayList, and so it is insufficient). Any ideas would be appreciated. Mark 11 Feb 2007
Instead of returning the
ArrayList
itself you could return an enumerator for theArrayList
. Take a look at theArrayList.GetEnumerator
method.
"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." - Rick Cook
-
I have a class with a function that returns an ArrayList. The problem is that I don't want any client that calls the function to be able to add or delete items from the ArrayList. I want to force the client to use my class' other functions to make any modifications (because related data has to be updated). Unfortunately, speed is an issue, so I can't just copy the ArrayList and send the copy. Also, a copy would allow the client to think he is making a change when it is actually being ignored. I'd prefer him to get a compile error if he calls Add(), for instance. I can't find any C# syntax that can make a returned value (or a returned parameter) read only. (The C# "readonly" syntax won't let the client replace the ArrayList with another, but WILL let the client change the content of the ArrayList, and so it is insufficient). Any ideas would be appreciated. Mark 11 Feb 2007
If the only action you want to offer is enumeration, return an enumerator. If you want to offer a larger subset of ArrayList functionality, create your own class that: - either inherits from ArrayList and hides the properties/methods you dont want to offer, - or contains an ArrayList and makes public the enumerator plus those extra properties/ methods you want to export. :)
Luc Pattyn
-
If the only action you want to offer is enumeration, return an enumerator. If you want to offer a larger subset of ArrayList functionality, create your own class that: - either inherits from ArrayList and hides the properties/methods you dont want to offer, - or contains an ArrayList and makes public the enumerator plus those extra properties/ methods you want to export. :)
Luc Pattyn
Thanks to you both who responded. I couldn't figure it out without you. For others interested, here is a minimal solution class which seems to work so far. You may want to add other properties or functions, but this is what I need for now. I call it a Firm List since, for the user only, the array size is no longer variable but is firm (solidified). The user can read and use each item in the list, but can't make the list contain a different item nor change the number of items in the list.
using System;
using System.Collections;namespace FreeForAll {
public class FirmList : IEnumerable {
ArrayList foundation;public FirmList(ArrayList source) { foundation = source; } public IEnumerator GetEnumerator() { return foundation.GetEnumerator(); } public bool Contains(object item) { return foundation.Contains(item); } public int Count { get { return foundation.Count; } }
}
}Here is how, within another class, I return a FirmList as property "SomeList". Within this class, I have full accees to the ArrayList itself and all of its abilities.
class A {
ArrayList TheRealList = new ArrayList();
FirmList TheFirmList;
A() { // constructor
TheFirmList = new FirmList(TheRealList);
}
...
void SomeFunctionThatAddsMembersToTheArrayList() {...};
...
public FirmList SomeList {
get { return TheFirmList; }
}
}And, finally, here is how some other code might use it, just like and ArrayList is used:
A aaa = new A();
aaa.SomeFunctionThatAddsMembersToTheArrayList();
FirmList list = A.SomeList;
foreach (object o in list) {
...
}
WriteLine("{0}", list.Count);
if (list.Contains(...)) {...Hope this helps someone else. Mark
-
I have a class with a function that returns an ArrayList. The problem is that I don't want any client that calls the function to be able to add or delete items from the ArrayList. I want to force the client to use my class' other functions to make any modifications (because related data has to be updated). Unfortunately, speed is an issue, so I can't just copy the ArrayList and send the copy. Also, a copy would allow the client to think he is making a change when it is actually being ignored. I'd prefer him to get a compile error if he calls Add(), for instance. I can't find any C# syntax that can make a returned value (or a returned parameter) read only. (The C# "readonly" syntax won't let the client replace the ArrayList with another, but WILL let the client change the content of the ArrayList, and so it is insufficient). Any ideas would be appreciated. Mark 11 Feb 2007
In C# you can't mark reference types as readonly. However you can Encapsulate ArrayList inside a class and provide restricted access to it through methods, properties. You mentioned that speed is an issue otherwise you could have simply returned a clone to it.