473,626 Members | 3,936 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Capturing Dictionary<T>.A dd() 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 21364
I went ahead with adding an Add() event handler. If any one is
interested:

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

public CustomDictionar y()
{

}

public void Add(TKey pKey, TValue pValue)
{
if( AddEvent != null )
AddEvent( new AddEventArgs(pK ey, 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 CustomDictionar y<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,T Value>(
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.AddEvent Handler AddEvent;

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

{
if( AddEvent != null )
Mydict.AddEvent ( new AddEventArgs(pK ey, 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

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

Similar topics

1
20092
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 content in the iframe it seems like the eventlistner is destroyed. I can´t really understand why. Because the onkeydown listner in the mainpage is untouched. Here is the code. Is there a solution to my problem? <body>
6
11326
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 = (function(event){alert('helloAgain')});"> <div id="testDiv" onMouseOver="alert('hello');">JaJa!</div>
3
8847
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 object? // get inputElement alert("Some java script code"); inputElement.onkeyup = ????;
6
1497
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 messages are shown in the pick dialog box....any help is appreicated Thanks jack
2
3735
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 takes the control, the event and the function as parameters, something like: function addevent(control as object, event as string, functionname as string)
1
1846
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 user control into the PlaceHolder control using this code... objPlaceHolder.Controls.Clear(); objPlaceHolder.Controls.Add(LoadControl("nnn.ascx")); where nnn.ascx is the appropiate control. On this user control I have a button and hander...
0
1383
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 loaded. From this BHO, i want to capture the button click event and take appropriate action.How to do this? Does some method exist already in this regard? Somebody pls help me.. With Regards, Sujoan.
3
2030
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 help on this issue.. or pls help me out if any other alternative exists in capturing a button click event from a BHO in IE.. Thanks and Regards, Sujoan.
3
7836
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 this? I know that i can make my app a windows app and hide the window - right now, this is not enough of a reason to go this route. And in that case, i would have to have the user to go to the task manager or provide another means to shutdown...
0
8266
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8705
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8638
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8365
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7196
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.