How to pass more than two parameters in the event handler

  • Thread starter Thread starter Swappy
  • Start date Start date
S

Swappy

Can anyone suggest me to pass more parameters other than two parameter for
events like the following?

Event:
Onbutton_click(object sender, EventArgs e)"

Event handler:
button.Click += new EventHandler(Onbutton_click);

I want to pass more information related that event. & want to use that
information in the event.
 
Swappy said:
Can anyone suggest me to pass more parameters other than two parameter for
events like the following?

Event:
Onbutton_click(object sender, EventArgs e)"

Event handler:
button.Click += new EventHandler(Onbutton_click);

I want to pass more information related that event. & want to use that
information in the event.

The normal way of passing more information is to make a richer type
derived from EventHandler.
 
Hey,
Jon Skeet is right with his suggestion about deriving from event args and
making you own delegate for the event. That is the standard way .NET does it
and there is necessarily anything wrong with it.
There is a bit of debate about it though as some people suggest that it is
not ideal to parse a reference to the sender and an EventArgs derived class
to a set of event handlers that only need to know a boolean or and int. They
believe that having lots of derivations of EventArgs introduces unnecessary
code bloat and is a waste of time.
I am personally undecided on the matter as although I like the idea of
having a standard 'look' for events, I also agree with the simplicity of just
parsing the needed data for events. The only down side of this is if you do
decide in the future to add a second piece of data, you need to change the
event and therefore all the subscribers. EventArgs does get round that for
you. For futher information on this argument and events in general, there was
recently a dotnetrocks episode on them:
http://www.dotnetrocks.com/default.aspx?showNum=355

I will leave it to you to come up with the solution yourself. Just thought I
would make you aware of the two sides to the debate.
 
Hey Swappy, I see three MVP's (Peter is also an MVP but he doesn't
like to talk about it) have answered your question, so far be it for
me to add my 0.02 dollars, but I would say that you should think twice
about passing parameters through Events other than as provided by
MSFT's wizards. Rule of thumb: don't mess with wizard generated
code, ever.

So I guess (if I read correctly) that I'm with Ciaran when he said:
"there is a bit of debate about it though as some people suggest that
it is not ideal to parse a reference to the sender and an EventArgs
derived class
to a set of event handlers that only need to know a boolean or and
int. They believe that having lots of derivations of EventArgs
introduces unnecessary code bloat and is a waste of time". Plus it's
dangerous--you mess up the library you have to reinstall Visual Studio
and that takes several hours.

So, I would set up a user defined "Event" using a publisher-subscriber
model--I posted recently in this forum an excellent console mode
example of this--rather than using the Event handler stuff generated
by the code wizard.

RL

( a newbie with a good month's worth--but a solid month--of C#
experience)
 
On Jul 16, 10:47 pm, Swappy <[email protected]> wrote:
int. They believe that having lots of derivations of EventArgs
introduces unnecessary code bloat and is a waste of time". Plus it's
dangerous--you mess up the library you have to reinstall Visual Studio
and that takes several hours.

I'm curious, how could deriving your own class from EventArgs possibly
"mess up the library" causing you to have to re-install VS?

Chris
 
Comments:
Hey Swappy, I see three MVP's (Peter is also an MVP but he doesn't
like to talk about it) have answered your question, so far be it for
me to add my 0.02 dollars,
errrrm.
I am NOT an MVP, and according to the Microsoft MVP awardies page, Peter
Duniho is not a current MVP holder
(https://mvp.support.microsoft.com/communities/mvp.aspx?name=peter+duniho).
but I would say that you should think twice
about passing parameters through Events other than as provided by
MSFT's wizards. Rule of thumb: don't mess with wizard generated
code, ever.
Events in .NET are not normally generated by wizards. They are made with
code like:
public delegate void MyEventHandler(object sender, MyEventEventArgs e);
public event MyEventHandler MyEvent;
There is nothing autogenerated about the above code. It is just C# written
by hand in this windows to create an delegate type, and an event.
So I guess (if I read correctly) that I'm with Ciaran when he said:
"there is a bit of debate about it though as some people suggest that
it is not ideal to parse a reference to the sender and an EventArgs
derived class
to a set of event handlers that only need to know a boolean or and
int. They believe that having lots of derivations of EventArgs
introduces unnecessary code bloat and is a waste of time".

This was a point of view that I raised as Devils Advocate rather than
voicing me own opinion. I tend to think of the phrase "Horses for courses",
as in, nothing is applicable everywhere.
Plus it's
dangerous--you mess up the library you have to reinstall Visual Studio
and that takes several hours.
Nothing you do in the code editor can mess this up unless you mean
attempting to edit Visual Studio's resource files which is not at all
something somebody should be doing. Creating events is a simple thing you do
in code.

So, I would set up a user defined "Event" using a publisher-subscriber
model--I posted recently in this forum an excellent console mode
example of this--rather than using the Event handler stuff generated
by the code wizard.
If this was your "UnAwareClass" based example then I would be concerned
reading that by the comment chain posted in that thread, that you are still
trying to push it as an "excellent" example. No offence to yourself, as I
think your willingness to take part in the community is respectable, but if
clever people like MVP's have so much to say on your example, maybe it isnt
"excellent".
 
Comments:


errrrm.
I am NOT an MVP, and according to the Microsoft MVP awardies page, Peter
Duniho is not a current MVP holder
(https://mvp.support.microsoft.com/communities/mvp.aspx?name=peter+duniho).

I KNEW IT--PETER DUNIHO IS A FRAUD! HAHAHAHAHA THANKS CIARAN! HAHAHA
A FRAUD. But he's more knowledgeable than most about C# so let's not
rub it in. Love it. A fake. Like that guy on Wikipedia that claimed
to be a college professor on some subject and was unemployed trailer
park trash.
Events in .NET are not normally generated by wizards. They are made with
code like:
public delegate void MyEventHandler(object sender, MyEventEventArgs e);
public event MyEventHandler MyEvent;
There is nothing autogenerated about the above code. It is just C# written
by hand in this windows to create an delegate type, and an event.

Sorry, I was not clear. I am saying the same thing, but saying that
when you click on an event handler like "_OnClick" using the Wizards,
the Events skeleton code is automatically generated by the Wizards,
that's all. I completely agree with you.

Wizards are great BTW-I'm using one now to create a MDI parent window
and child windows under Forms--great stuff.
This was a point of view that I raised as Devils Advocate rather than
voicing me own opinion. I tend to think of the phrase "Horses for courses",
as in, nothing is applicable everywhere.

So that's what that phrase means. Always wondered about that.
Nothing you do in the code editor can mess this up unless you mean
attempting to edit Visual Studio's resource files which is not at all
something somebody should be doing. Creating events is a simple thing youdo
in code.

Right. I meant if you mess with the resource files and/or .h headers
in C++ that are in the library--don't go there.
If this was your "UnAwareClass" based example then I would be concerned
reading that by the comment chain posted in that thread, that you are still
trying to push it as an "excellent" example. No offence to yourself, as I
think your willingness to take part in the community is respectable, but if
clever people like MVP's have so much to say on your example, maybe it isnt
"excellent".

But some of them are imposter MVP's. That don't count. Is Jon Skeet
an MVP? Lemme check...he's been posting forever, I see his name from
eight years ago on the net...well I'll be damned, not only is he an
MVP, he's almost famous: "Jon is primarily a C# and Java developer in
Reading, England. In 2008 Manning published his book, "C# in Depth."
He has three young sons, and is currently working at Google".
Google, man I remember when they went public, and how the smart money
in Silcon Valley where I was at the time piled into those restricted
shares--they knew a good thing and made off like bandits.

RL

[N00b C# MVP, with a solid 30 days coding experience]
 
raylopez99 said:
I KNEW IT--PETER DUNIHO IS A FRAUD! HAHAHAHAHA THANKS CIARAN! HAHAHA
A FRAUD. But he's more knowledgeable than most about C# so let's not
rub it in. Love it. A fake. Like that guy on Wikipedia that claimed
to be a college professor on some subject and was unemployed trailer
park trash.

Nope, that's just not a reliable way of deciding if someone's an MVP or
not. It just means that if Peter is an MVP (which I happen to know he
is, or at the very least *was*) then he's made his profile non-public.

It's easy to demonstrate that that's the case. You've just posted about
my public profile - go and see if you can find it again. I've just made
it non-public, and I think you'll find that if you look now, you won't
see it. The awardee directory does state fairly plainly: "The Microsoft
MVP Awardee directory contains a listing of all the MVPs that want to
share their information publicly."

Let me know when you've confirmed that, and I'll make my profile public
again...

It's a shame there isn't a "verify that XYZ is an MVP without showing a
profile" option, but that's just the way it goes.

My complete *guess* is that Peter won't go to the trouble of making his
profile public just to make a point, but I sincerely hope that if he
does so, you are ready to apologise for the insults quoted above.
 
Sorry about the mistake Peter. I'll add it to the list of many times I should
have read the small print to avoid an embarassing or costly mistake.

Sincerest Apologies
 
My complete *guess* is that Peter won't go to the trouble of making his
profile public just to make a point, but I sincerely hope that if he
does so, you are ready to apologise for the insults quoted above.

I confirmed your public profile was set to private, so you can turn it
back on now.

Peter doesn't want my apology--he's a tough old coot and enjoys the
'intellectual repartee' of sparring with somebody who is at least his
equal in smarts, and in coding maybe will be some day!

RL
 
I confirmed your public profile was set to private, so you can turn it
back on now.

Thank you.
Peter doesn't want my apology--he's a tough old coot and enjoys the
'intellectual repartee' of sparring with somebody who is at least his
equal in smarts, and in coding maybe will be some day!

Leaving your assertion of being "at least [Peter's] equal in smarts"
to one side, are you seriously happy to call someone a fraud and not
apologise for it when you know you're wrong? Part of having a
civilised debate is respecting those you're debating with - and part
of showing respect is apologising when you're wrong. That's true even
for technical inaccuracies, but I'd say it's doubly important when
you've made inaccurate personal assertions.

Jon
 
I'm happy to ignore those attacks; after all, they say a lot more about  
you than they do about me.  But a person deserving of community respect 
knows when they've crossed the line and _sincerely_ apologizes for it.

OK, OKAY. Jeez I didn't realise you such a shrinking violet.

"I sincerely appologize" for flaming you. happy now?

See my followup post BTW on delegates in the .NET framework--I've
discovered (for me) a shocking non-standard way that events are fired
when using the System.EventArgs class, which apparently is manditory,
as is the "On" prefix. Shocking stuff.

GIve you a quick preview: these below lines are IDENTICAL!!!


this.buildProgress += BuildProgressHandler; //BuildProgressHander is
a function that returns void and takes parameters (object, int)
//equivalent to

this.buildProgress += new BuildProgress(BuildProgressHandler);

delegate void BuildProgress(object sender, int progressPercent); //

// this.BuildComplete += BuildCompleteHandler; //note
format: name of event on LHS and 'naked' format method name on RHS!!!

this.BuildComplete += new
EventHandler(BuildCompleteHandler);

//EventHandler defined by .NET!!! (EventArgs is the base class)

Shocking stuff. I dont have time to get into it here.

RL
 
Until you fully comprehend what I actually wrote, I don't believe it will 
be possible for you to offer a sincere apology.  And it's clear you don't  
comprehend what I wrote.

I'm REALLY sorry, OK? Besides I need guys like you to help me learn
C# and debug my code when I run into a brick wall, so I have to be
nice--self interest you know. So far though, I'm not having too many
problems with C# Forms, which seem to be more robust than the GUI
database stuff for C# (the frequent casting there seems terrible and
undocumented, and stuff is always not compiling even when copied from
a book).

RL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top