473,397 Members | 1,960 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,397 software developers and data experts.

Show only needed methods from a Dictionary<TKey, TValue> class member

The context is that I have a C# class named MainModel which has a
private Dictionary<string, FileStreammember named dict. I also have
a property Dict to access to this member:

public Dictionary<string, FileStreamDict
{
get { return this.dict; }
}

The problem with this implementation is that I only want to give
access to some of the methods from the Dictionary class since I don't
want another class which has a MainModel instance to have full access
to the dict member and do whatever he wants to. What should I do in
this case?

I'm not an advanced programmer and I'm not too familiar with the
design patterns, so I would like to have some code to base on and some
explainations too.

What I would like to have is somekind of:

public DictionaryExhibitor<Dictionary<string, FileStream>Dict
{
get { return DictionaryExhibitor<this.dict>; }
}

Please correct me if I'm wrong.

Thanks
WuFei

May 29 '07 #1
6 1799
Various options; you could expose the dictionary as the interface
IDictionary<string, FileStreamrather than the concrete
Dictionary<string, FileStream>, but this doesn't gain you much since a
determined borker can still investiagte GetType() and cast etc...

What access *do* you want to expose? If you just want to expose the
values, then perhaps an indexer is the answer?

// (notepad code; untested)
public FileStream this[string key] {
get {return dict[key];}
}

Now the dictionary is not visible outside *at all*. If you want a
middle ground of access members, then yes some form of wrapper class
would seem in order - although it could probably be (using your terms)
DictionaryExhibitor<TKey,TValue(i.e.
DictionaryExhibitor<string,FileStream>) rather than
DictionaryExhibitor<Dictionary<string,FileStream>> , and might take (as
an example) an IDictionary<string,FileStreamas a constructor
argument (meaning: it will work with any dictionary-esque object).

Marc
May 29 '07 #2
Thanks Marc for your fast reply. It helped a lot.
By the way, which base class(es) should my class
DictionaryExhibitor<TKey, TValueinherit from? Only
IDictionary<TKey, TValueor some other classes too?

DictionaryExhibitor<TKey, TValue: IDictionary<TKey, TValue>

For those methods I want to hide, should I simply implement them by
putting an empty body? For example, the Clear() method:

public void Clear() { }

Thanks
WuFei

On May 29, 4:48 pm, Marc Gravell <marc.grav...@gmail.comwrote:
Various options; you could expose the dictionary as the interface
IDictionary<string, FileStreamrather than the concrete
Dictionary<string, FileStream>, but this doesn't gain you much since a
determined borker can still investiagte GetType() and cast etc...

What access *do* you want to expose? If you just want to expose the
values, then perhaps an indexer is the answer?

// (notepad code; untested)
public FileStream this[string key] {
get {return dict[key];}

}

Now the dictionary is not visible outside *at all*. If you want a
middle ground of access members, then yes some form of wrapper class
would seem in order - although it could probably be (using your terms)
DictionaryExhibitor<TKey,TValue(i.e.
DictionaryExhibitor<string,FileStream>) rather than
DictionaryExhibitor<Dictionary<string,FileStream>> , and might take (as
an example) an IDictionary<string,FileStreamas a constructor
argument (meaning: it will work with any dictionary-esque object).

Marc

May 31 '07 #3
Let me turn that around: what functionality do you want to expose?
What must it do? But yes, the IDictionary<TKey, TValuewould seem a
good candidate.

For instance, if you (for whatever reason) need to implement a
specific interface, then you can use "explicit implementation" for the
ones you want to hide, and perhaps throw a NotSupportedException or
InvalidOperationException. If it can't be used, there is no point
exposing it on the public class definition.

Marc
May 31 '07 #4
On May 31, 3:00 pm, daohuy....@gmail.com wrote:
Thanks Marc for your fast reply. It helped a lot.
By the way, which base class(es) should my class
DictionaryExhibitor<TKey, TValueinherit from? Only
IDictionary<TKey, TValueor some other classes too?
Potentially, nothing. If you only want to expose very limited access,
I'd just start from scratch. Work out what clients will need, and put
that into your class. If you find you've naturally implemented a whole
interface, it probably wouldn't hurt to add that to the list of
interfaces you actually implement.

Jon

May 31 '07 #5
Thanks Marc and Jon. It's one of my first time I'm writing an
interface, so I did want to ensure that I'm doing the right things.
Really appreciate to get answers from experimented people.

WuFei

On May 31, 10:12 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
Let me turn that around: what functionality do you want to expose?
What must it do? But yes, the IDictionary<TKey, TValuewould seem a
good candidate.

For instance, if you (for whatever reason) need to implement a
specific interface, then you can use "explicit implementation" for the
ones you want to hide, and perhaps throw a NotSupportedException or
InvalidOperationException. If it can't be used, there is no point
exposing it on the public class definition.

Marc

Jun 1 '07 #6
Thanks Marc and Jon. Since I'm pretty new to C# and to design
patterns, I did want to make sure I'm doing the right things.
It's appreciated to get advices from experimented people.

WuFei

On May 31, 10:12 am, "Marc Gravell" <marc.grav...@gmail.comwrote:
Let me turn that around: what functionality do you want to expose?
What must it do? But yes, the IDictionary<TKey, TValuewould seem a
good candidate.

For instance, if you (for whatever reason) need to implement a
specific interface, then you can use "explicit implementation" for the
ones you want to hide, and perhaps throw a NotSupportedException or
InvalidOperationException. If it can't be used, there is no point
exposing it on the public class definition.

Marc

Jun 1 '07 #7

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

Similar topics

12
by: Brett Romero | last post by:
If I want to take action on the Add event of a generic Dictionary, do I need to create a custom Dictionary and add an event handler for the Add() method? The dictionary is a public field on a...
2
by: Shimon Sim | last post by:
I have Dictionary that has custom class as its key. For that class I implemented both Equals(object) and IComparable<T>. I keep getting KeyNotFoudException. What should I do? Thanks Shimon
7
by: Russell Hind | last post by:
I want to create an array of key value pairs. This code compiles fine: KeyValuePair<string, object> kvps = { new KeyValuePair<string,object>( "int", 5 ), new KeyValuePair<string,object>(...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that expects a List<string>. I often do the following: ...
7
by: bonk | last post by:
Hello I am acessing a Dictionary<TKey,TValuefrom multiple threads and often in a foreach loop. While I am within one of the foreach loops the other threads must not modify the collection itself...
2
by: Steve Richter | last post by:
KeyValuePair<string,stringhas a ToString method that returns the KeyValue pair seperated by a comma and enclosed in : Is this method used as a building block for serialization? The reason I...
44
by: Zytan | last post by:
The docs for List say "The List class is the generic equivalent of the ArrayList class." Since List<is strongly typed, and ArrayList has no type (is that called weakly typed?), I would assume...
4
by: Peter K | last post by:
Hi are there any benefits in using StringDictionary over Dictionary<string, string? It appears they achieve the same thing... (I could be wrong of course). thanks, Peter
2
by: Dave | last post by:
I seem to be missing one of my favorite STL techniques for key, value collections - being able to use Composite Keys. What i mean by C.K. is the ability for a key to compare multiple values when...
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: 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
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...
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.