473,406 Members | 2,620 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,406 software developers and data experts.

Read-Only Hashtable - how?

Hashtable has a property IsReadOnly. i can't figure out how this gets set.
It's not a property you can set directly and i can't find a constructor that
takes an RO parameter. How on earth does it get set?

More generally, is there a way i can expose a stanadrd collection as
read-only? Manager should be able to load and modify Employees internally but
when exposing it to the caller it should not let the caller change it.

i can do this by writing my own custom class (and maybe inheriting
ReadOnlyCollectionBase) but i'd rather use a built in class if one is
available

i considered keeping a normal Hashtable (or somesuch) internally and
re-casting it or cloning it to something readonly to expose but didn't find
anything suitable

i thought for a second i could add the keyword readonly in front of the
variable declaration but i have since learned the error of my ways

So is there a way to use one of the built-in collections to expose read-only
data or do i need to create my own?

-baylor
Jul 21 '05 #1
1 3013
baylor wrote:
Hashtable has a property IsReadOnly. i can't figure out how this gets set. i can do this by writing my own custom class (and maybe inheriting
ReadOnlyCollectionBase) but i'd rather use a built in class if one is
available
You will need to proxy the collection.
Note that you need to trust the caller not to downcast the Values, Keys,
GetEnumerator(), ... if you don't want to proxy those.

Below is some exaple code you can use as:

IDictionary dict = some_dictionary_which_may_be_read_write;
return ReadOnlyDictionary.Make(dict);
class ReadOnlyDictionary: IDictionary
{
public static IDictionary Make(IDictionary dict)
{
if ( dict.IsReadOnly )
return dict;
else
return new ReadOnlyDictionary(dict);
}
class Immutable: NotSupportedException
{ public Immutable(): base("Immutable data-structure") {} }
protected IDictionary Parent;
protected ReadOnlyDictionary(IDictionary dict)
{ this.Parent = dict; }
#region IDictionary Members
public bool IsReadOnly { get { return true; } }
/* IDictionaryEnumerator doesn't provide mutation
so we can reuse parents without proxy
if we trust the caller not to downcast
to the real type
*/
public IDictionaryEnumerator GetEnumerator()
{ return Parent.GetEnumerator(); }
public object this[object key]
{
get { return Parent[key]; }
set { throw new Immutable(); }
}
public void Remove(object key) { throw new Immutable(); }
public bool Contains(object key) { return Parent.Contains(key); }
public void Clear() { throw new Immutable(); }
/* ICollection doesn't provide mutation
so we can reuse parents without proxy
if we trust the caller not to downcast
to the real type
*/
public ICollection Values { get { return Parent.Values; } }
public void Add(object key, object value) { throw new Immutable(); }
/* ICollection doesn't provide mutation
so we can reuse parents without proxy
if we trust the caller not to downcast
to the real type
*/
public ICollection Keys { get { return Parent.Keys; } }
public bool IsFixedSize { get { return Parent.IsFixedSize; } }
#endregion

#region ICollection Members
public bool IsSynchronized
{ get { return Parent.IsSynchronized; } }
public int Count { get { return Parent.Count; } }
public void CopyTo(Array array, int index)
{ Parent.CopyTo(array, index); }
public object SyncRoot { get { return Parent.SyncRoot; } }
#endregion

#region IEnumerable Members
IEnumerator System.Collections.IEnumerable.GetEnumerator()
{ return ((IEnumerable)Parent).GetEnumerator(); }
#endregion

public override bool Equals(object obj)
{ return Parent.Equals(obj); }
public override int GetHashCode() { return Parent.GetHashCode(); }
public override string ToString() { return Parent.ToString(); }
}
So is there a way to use one of the built-in collections to expose read-only
data or do i need to create my own?


Just use a proxy, it's rather convinient.

Remember, that this doesn't guard you against people using reflection or
whatnot. The only alternative is to copy your returned hashtable's, that
way you don't *care* if the caller edits it.

return new Hashtable(dict);

But that's awfully expensive.

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Jul 21 '05 #2

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

Similar topics

2
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My...
6
by: Steve | last post by:
Hi, I'm trying to convert a file reading loop into one using streams. The BSD OS read API returns the number of bytes read, but istream::read returns itself. How can I find out the number of...
12
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I...
2
by: Sandman | last post by:
Just looking for suggestion on how to do this in my Web application. The goal is to keep track of what a user has and hasn't read and present him or her with new material I am currently doing...
4
by: Ollie Cook | last post by:
Hi, I am having some difficulty with read(2) and interrupting signals. I expect I am misunderstanding how the two work together, so would appreciate some guidance. I am trying to 'time out' a...
1
by: Jose Reckoner | last post by:
I'm running python 2.3 on Windows XP. Anyone have a quick small script to convert .DT1 and .DEM data to ASCII or some other format? I don't need a viewer. Thanks!
0
by: phplasma | last post by:
Hey, I am currently attempting to implement a multi-threaded C# socket, using SSL (.pem file/certification/private key combo) server using Visual Studio C# Express. I have successfully made...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
4
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have...
5
by: Thomas Christensen | last post by:
This issue has been raised a couple of times I am sure. But I have yet to find a satisfying answer. I am reading from a subprocess and this subprocess sometimes hang, in which case a call to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.