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

Immutable Hashtable....

Can anyone tell me how to make a Hashtable immutable? I see that
Hashtable has the "IsReadOnly" property, but I can't see how I am
supposed to actually make it immutable.

Thanks!
BCOT.
Nov 16 '05 #1
4 2955
"Johnny Cash" schrieb
Can anyone tell me how to make a Hashtable immutable? I see that
Hashtable has the "IsReadOnly" property, but I can't see how I am
supposed to actually make it immutable.


Hi Johnny,

HashTable.IsReadOnly will always be false by default.

If you want an "immutable" HashTable then darive a new Class from HashTable
and override the IsReadOnly-Property.

Look at the foolowing code:

using System;
using System.Collections;
public class ReadOnlyHashTable : Hashtable
{
private bool _IsReadOnly = true;
public ReadOnlyHashTable()
{
}

public ReadOnlyHashTable(bool IsReadOnly)
{
_IsReadOnly = IsReadOnly;
}

internal void SetReadOnly(bool readOnly)
{
_IsReadOnly = readOnly;
}

public override bool IsReadOnly
{
get
{
return _IsReadOnly;
}
}

public override void Add(object key, object value)
{
if (_IsReadOnly)
{
throw new Exception("ReadOnly!");
}
base.Add (key, value);
}

public override object this[object key]
{
get
{
return base[key];
}
set
{
if (_IsReadOnly)
{
throw new Exception("ReadOnly!");
}
base[key] = value;
}
}
}

Cheers

Arne Janning
Nov 16 '05 #2
Arne... This code looks a bit suspect to me as it uses inheritance, not
containment. This is what the docs say:

==== snip =====
A collection that is read-only does not allow the addition, removal, or
modification of elements after the collection is created.

A collection that is read-only is simply a collection with a WRAPPER
that
prevents modifying the collection; therefore, if changes are made to the
underlying collection, the read-only collection reflects those changes.
=====/snip=======

http://www.parashift.com/c++-faq-lit...heritance.html

[21.1] Should I hide member functions that were public in my base class?

Never, never, never do this. Never. Never!

Attempting to hide (eliminate, revoke, privatize) inherited public
member
functions is an all-too-common design error. It usually stems from muddy
thinking.

More at:

http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple

Regards,
Jeff
If you want an "immutable" HashTable then darive a new Class from

HashTable
and override the IsReadOnly-Property.

public override void Add(object key, object value)
{
if (_IsReadOnly)
{
throw new Exception("ReadOnly!");
}
base.Add (key, value);
}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
Hi Jeff!

"Jeff Louie" schrieb
Arne... This code looks a bit suspect to me as it uses inheritance, not
containment. This is what the docs say:

==== snip =====
A collection that is read-only does not allow the addition, removal, or
modification of elements after the collection is created.

A collection that is read-only is simply a collection with a WRAPPER
that
prevents modifying the collection; therefore, if changes are made to the
underlying collection, the read-only collection reflects those changes.
=====/snip=======

http://www.parashift.com/c++-faq-lit...heritance.html
I don't see what this has to do with my code...
[21.1] Should I hide member functions that were public in my base class?

Never, never, never do this. Never. Never!

Attempting to hide (eliminate, revoke, privatize) inherited public
member
functions is an all-too-common design error. It usually stems from muddy
thinking.

More at:

http://c2.com/cgi/wiki?LiskovSubstitutionPrinciple


I didn't hide or eliminate a public member. Actually this isn't possible in
C#.

What is your problem?

Cheers

Arne Janning

Nov 16 '05 #4
Arne...Well 1) Your solution uses inheritance to convert a read/write
interface
to read only. I argue that this should be done with containment.

http://www.geocities.com/jeff_louie/OOP/oop7.htm

The MS docs argue that this should be done with containment.
"A collection that is read-only is simply a collection with a WRAPPER
that prevents modifying the collection."

2) Throwing an exception instead of providing an implementation violates
the
Liskov Substitution Principle--> The subclass may add behaviors to the
base
class, but the behaviors of the base class must still hold true for the
subclass.
In real world terms, the user of the class expects to be able to add an
element
and does not expect the add method to throw an exception. The contract
is
broken at runtime. Wrapping the collection is a wrapper class and
providing
only setters, results in compile time enforcement of the read only
contract.
I didn't hide or eliminate a public member. Actually this isn't possible in
C#.<

3) "Attempting to hide (eliminate, revoke, privatize) inherited public
member
functions is an all-too-common design error. It usually stems from muddy
thinking." By throwing an exception you revoked the contract.
What is your problem?<


Nice touch.

Regards,
Jeff
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #5

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

Similar topics

2
by: Zalek Bloom | last post by:
I am learning about difference between String and StringBuffer classes. In a book it sayes that a String class is immutable, that means that one an instance of String class is created, the string...
7
by: Torsten Mohr | last post by:
Hi, reading the documentation (and also from a hint from this NG) i know now that there are some types that are not mutable. But why is it this way? From an overhead point of view i think...
48
by: Andrew Quine | last post by:
Hi Just read this article http://www.artima.com/intv/choices.html. Towards the end of the dicussions, when asked "Did you consider including support for the concept of immutable directly in C#...
5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
22
by: Ben Finney | last post by:
Howdy all, I've recently packaged 'enum' in PyPI. In its description, I make the claim that it creates "immutable" enumeration objects, and that the enumeration values are "constant" values. ...
90
by: Ben Finney | last post by:
Howdy all, How can a (user-defined) class ensure that its instances are immutable, like an int or a tuple, without inheriting from those types? What caveats should be observed in making...
3
by: Sam Kong | last post by:
Hi group, I want to have some advice about immutable objects. I made a constructor. function Point(x, y) { this.x = x; this.y = y; }
2
by: PAzevedo | last post by:
I have this Hashtable of Hashtables, and I'm accessing this object from multiple threads, now the Hashtable object is thread safe for reading, but not for writing, so I lock the object every time I...
16
by: InDepth | last post by:
Now that .NET is at it's fourth release (3.5 is coming soon), my very humble question to the gurus is: "What have we won with the decision to have string objects immutable? Or did we won?" ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.