473,399 Members | 4,254 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,399 software developers and data experts.

Capturing Dictionary<T>.Add() event?

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

Thanks,
Brett

May 23 '06 #1
12 21282
I went ahead with adding an Add() event handler. If any one is
interested:

public class CustomDictionary<TKey, TValue> : Dictionary<TKey,
TValue>
{
public event AddEventHandler AddEvent;

public CustomDictionary()
{

}

public void Add(TKey pKey, TValue pValue)
{
if( AddEvent != null )
AddEvent( new AddEventArgs(pKey, pValue) );
base.Add( pKey, pValue );
}

public delegate void AddEventHandler( AddEventArgs
pAddEventArgs );

public class AddEventArgs : EventArgs
{
private TKey _key;
private TValue _value;

public AddEventArgs( TKey key, TValue value )
{

}

public TKey Key
{
get
{
return _key;
}
}

public TValue Value
{
get
{
return _value;
}
}
}
}
Brett

May 23 '06 #2
"Brett Romero" <ac*****@cygen.com> wrote:
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 custom control.


The existing Dictionary<,> generic class uses non-virtual methods for
speed reasons, for those people who need the speed.

If you want to intercept these kinds of events, consider descending from
the KeyedCollection<,> class, or write your own class that implements
IDictionary<,> and delegates actual storage to a Dictionary<,>.

-- Barry

--
http://barrkel.blogspot.com/
May 23 '06 #3
"Brett Romero" <ac*****@cygen.com> wrote:
I went ahead with adding an Add() event handler. If any one is
interested:

public class CustomDictionary<TKey, TValue> : Dictionary<TKey,
TValue>


Be careful: any client code can evade your event notification by simply
casting the dictionary to Dictionary<TKey,TValue> and calling Add()
directly. So, if you expect this event to be invoked every time the
Add() method is called on your public property, you will be
disappointed!

-- Barry

--
http://barrkel.blogspot.com/
May 23 '06 #4
>>If you want to intercept these kinds of events, consider descending from
the KeyedCollection<,> class, or write your own class that implements
IDictionary<,> and delegates actual storage to a Dictionary<,>.

I don't notice any difference in performance. Just not enough data or
use of this to make any difference. I don't need everything that
IDictionary<,> will require. Rewiring to work with KeyCollection<,>
isn't worth the effort and I'm unsure if it will in the end accomplish
what I already have. Inheriting from Dictionary<,> is doing
everything I need with good performance.

No one will be casting this either.

Thanks,
Brett

May 24 '06 #5
"Brett Romero" <ac*****@cygen.com> wrote:
If you want to intercept these kinds of events, consider descending from the KeyedCollection<,> class, or write your own class that implements
IDictionary<,> and delegates actual storage to a Dictionary<,>.


I don't notice any difference in performance.


It's not about performance: it's about code reliability. If you're
depending on your event being called, people can evade it by casting to
the base class.
No one will be casting this either.


If it's on a public interface, you can't guarantee anything about how
it's going to be used, unless it follows the rules of the CLR framework.

It's easy to cast by accident if you've got worker methods that work
with IDictionary<,>, for example. I do things like this all the time.
YMMV!

-- Barry

--
http://barrkel.blogspot.com/
May 24 '06 #6
I guess I'd need to see an example and explanation of why you'd want to
cast a generic. Isn't that defeating the purpose? It won't be casted
in my cast and I can guarantee that. The added complexity of a cast
will never be used.

Brett

Jun 1 '06 #7
"Brett Romero" <ac*****@cygen.com> wrote:
I guess I'd need to see an example and explanation of why you'd want to
cast a generic. Isn't that defeating the purpose? It won't be casted
in my cast and I can guarantee that. The added complexity of a cast
will never be used.


This is contrived to make a point:

---8<---
static public class Util
{
public static void AddEntry<TKey,TValue>(
Dictionary<TKey,TValue> dict, TKey key, TValue value)
{
dict.Add(key, value);
}
}
--->8---

Pass the dictionary that is part of the public interface of your class
to this method (Util.AddEntry), and it will avoid your hook on the Add
method.

The cast is implicit, and you can do nothing to guarantee it won't be
used if the class with this custom dictionary is a public part of a
library.

-- Barry

--
http://barrkel.blogspot.com/
Jun 4 '06 #8
Pass the dictionary that is part of the public interface of your class
to this method (Util.AddEntry), and it will avoid your hook on the Add
method.

The cast is implicit, and you can do nothing to guarantee it won't be
used if the class with this custom dictionary is a public part of a
library.


Not if you add the event there also:

static public class Util
{
public event Mydict.AddEventHandler AddEvent;

public static void AddEntry<TKey,TValue>(
Dictionary<TKey,TValue> dict, TKey key, TValue value)

{
if( AddEvent != null )
Mydict.AddEvent( new AddEventArgs(pKey, pValue) );
dict.Add(key, value);
}
}

I just can't think of a practical reason why you'd want to do such a
thing in the first place. Not to say one doesn't exists.

Brett

Jun 7 '06 #9
"Brett Romero" <ac*****@cygen.com> wrote:
Pass the dictionary that is part of the public interface of your class
to this method (Util.AddEntry), and it will avoid your hook on the Add
method.

The cast is implicit, and you can do nothing to guarantee it won't be
used if the class with this custom dictionary is a public part of a
library.


Not if you add the event there also:


You're missing the point - I, the client of *your* code, possibly on the
other side of the world and speaking in a different language, wrote the
AddEntry method *myself*. I have no responsibility to call *your* event.
That is what I mean by pointing out that you've got no guarantee that
*your* event will be called.

-- Barry

--
http://barrkel.blogspot.com/
Jun 8 '06 #10
Yes - you are correct when I have no control over the environment that
this code will be used in. However, I do have complete control so it
is a safe bet.

Thanks,
Brett

Jun 8 '06 #11
Hi Barry,
Hi everybody,

Barry Kelly ha scritto:
Be careful: any client code can evade your event notification by simply
casting the dictionary to Dictionary<TKey,TValue> and calling Add()
directly. So, if you expect this event to be invoked every time the
Add() method is called on your public property, you will be
disappointed!


I agree with your suggestion.
I think that the better way is to wrap a Dictionary<TKey,TValue> with a
wrapper class implementing the IDictionary<,> interface, declaring

private Dictionary<,> innerDictionary

within this new class. So that no one can access the member fo the inner
dictionary (without event handling). Am I right? I ask you because I'm
quite "new" in C# programming...

Thanks,
Giulio

Jun 8 '06 #12
Giulio Petrucci <gi*************@RIMUOVIspeechvillage.com> wrote:
Hi Barry,
Hi everybody,

Barry Kelly ha scritto:
Be careful: any client code can evade your event notification by simply
casting the dictionary to Dictionary<TKey,TValue> and calling Add()
directly. So, if you expect this event to be invoked every time the
Add() method is called on your public property, you will be
disappointed!


I agree with your suggestion.
I think that the better way is to wrap a Dictionary<TKey,TValue> with a
wrapper class implementing the IDictionary<,> interface, declaring

private Dictionary<,> innerDictionary

within this new class. So that no one can access the member fo the inner
dictionary (without event handling). Am I right? I ask you because I'm
quite "new" in C# programming...


Sure. And the KeyedCollection<T> class already does most of what most
people require, so that they simply need to override the InsertItem/ etc
methods.

-- Barry

--
http://barrkel.blogspot.com/
Jun 8 '06 #13

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

Similar topics

1
by: Tobias Åkeblom | last post by:
I have a mainpage where i display my menu and an iframe for the content to load in. I want to trace keydown events i the Iframe. This works well the first time I load the site. But when I load new...
6
by: Fredrik Celin | last post by:
If I add an event (to a div for example) with js, it replaces the event if there already is one. How can I add instead of replace this? Example: <body onLoad="testDiv.onmouseover =...
3
by: Trent | last post by:
Hi. I know the basic way to assign event handlers: <input onKeyUp="processEvent(event)" /> But how do I assign a function to the onKeyUp event in *javascript* that can access the event...
6
by: Jacky Luk | last post by:
I want to include some code for the MouseDown Event. I would like to add this for the formview or a picturebox, how come there are no messages for me to choose from. Even I created a button, no...
2
by: Jose Suero | last post by:
Hi all I have a dynamically created button, I can add an event handler with: AddHandler button.click, AddressOf static_function This works great, but what I need is to create a function that...
1
by: Ralph Krausse | last post by:
My UI is one table, 2 colums, one row. The cell on the left has a tree and the cell on the right has a placeholder control. When the user clicks on a tree item, I dynamically load the apporopiate...
0
by: Sujoan | last post by:
Hi, I have added a button to the standard toolbar in Internet Explorer.I have a Browser Helper Object(Basically gets the source code of the web page) that works fine everytime web page gets...
3
by: Sujoan | last post by:
Hi, What is WMI? Is it possible to use WMI event to capture a Toolbar button click(using RSOP_IEToolbarButton object) from a DLL(A Browser Helper Object in my project)?If so,please provide me some...
3
by: Meya-awe | last post by:
Hi, I have a console app, i want to be able to do some clean up when the user exits the application. So, i need to capture the red close window button event on the "cmd.exe" window. How can i do...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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.