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

will C# ever get a more declarative way of handling events? VB is gold on this issue

VB's more declarative nature of handling events is golden. I'm hoping C#
will acquire this type of deal, in addition to the anonymous delegates.

could do same as vb (actually would be easier to parse then vb due to
braces) whereas you look for Handles keyword after method sig, but before
opening brace of the method.

compiler would implicitly build event handling code for all typed
constructions, ie, any variables instances constructed via "new" operator,
compiler would follow with <instanceName>.<event> += new
<eventHandlerType>(<methodNameOfHandler>)

Seems like the algo could work like this

if "Handles" is found between method signature and first opening brace,
follow rules for dereferencing a simple expression of a type, followed by an
event name.
if expression is valid then
build implied code event handler creation for all found "new" operators
of the class
end if

c#:
private void Button_Click(object sender,EventArgs e) handles Button1.Click,
Button2.Click, Button3.Click
{
}
protected Button Button1;
private void InitializeComponent()
{
Button1 = new Button();
// implicit compiler event handling addition
Button1.Click += new EventHandler(Button1_Click); // statement is
implicit compiler addition
Button1.Text = "Button1";
// (finish building button1's properties)
Button2 = new Button();
// implicit compiler event handling addition
Button2.Click += new EventHandler(Button_Click);//statement is implicit
compiler add
Button2.Text = "Button2";

}

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]
Nov 15 '05 #1
2 2073
Eric Newton <er**@cc.ensoft-software.com> wrote:
VB's more declarative nature of handling events is golden. I'm hoping C#
will acquire this type of deal, in addition to the anonymous delegates.

could do same as vb (actually would be easier to parse then vb due to
braces) whereas you look for Handles keyword after method sig, but before
opening brace of the method.

compiler would implicitly build event handling code for all typed
constructions, ie, any variables instances constructed via "new" operator,
compiler would follow with <instanceName>.<event> += new
<eventHandlerType>(<methodNameOfHandler>)

Seems like the algo could work like this

if "Handles" is found between method signature and first opening brace,
follow rules for dereferencing a simple expression of a type, followed by an
event name.
if expression is valid then
build implied code event handler creation for all found "new" operators
of the class
end if


<snip>

While I can see some of the appeal, this strikes me as a really nasty
thing to happen in terms of the language. It's just not the kind of
thing the language should be trying to deal with. It would also be open
to problems - for instance, what if your button wasn't generated by a
"new" operator but by a method returning a button reference?

I'm sure there's an elegant solution around somewhere, but I don't know
what it is. Here's another idea though:

// Variable declaration
eventhandled Button button1;

// Method declaration
private void Button_Click(object sender,EventArgs e) handles
Button1.Click

generates code equivalent to:

Button _button1;
Button button1
{
get { return button1; }
set
{
if (button1 != null)
{
button1.Click -= new EventHandler (Button_Click);
}
button1 = value;
if (button1 != null)
{
button1.Click += new EventHandler (Button_Click);
}
}
}

In other words, the field is turned into a property and code is
generated to add/remove the event handlers whenever the value is
changed.

Things would get very sticky when it came to inheritance though - the
base class would only know about events within the class itself, so a
derived class couldn't "add" event handlers. Maybe that would be a
reasonable restriction though.

I'm not sure I like disguising a property as a field, but I feel it's a
bit nicer (and more reliable) than other ways. Mind you, it still has
problems:

Button tmp = button1;
button1 = new Button(...);
// The original button no longer has event handling; is this okay or
// not?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
I do see your point, and its something that I'm not completely comfortable
with either

However, the zeal of a more declarative event handling mechanism would make
it easier when transferring Control code from form to form, more similarly
like our VB counterparts.

Of note, the first notion of this came about from the C# designer tending to
"lose" the events when a control was cut and pasted... with the VB
declarative syntax, this is lessened, even though the VB designer tends to
screw that up too.

Its always annoying to cut/paste a control because of trying to get the
docking order correct, then finding out that you have to hook up all the
events again because the designer wiped them all out.

Incidentally, as I think about this more, I wonder if the same thing will
happen to the anonymous delegates? In theory it would, however, anonymous
delegates probably wouldnt show up in InitializeComponent and if they did
then I would blame to programmer for messing around in "auto generated" code
regions.

following VB code is very easy to cut/paste from form to form, (ignoring the
fact that I hate cut/copied and pasted code)

Dim WithEvents txtOcr as TextBox
Private Sub txtOcr_MouseUp(Byval sender as Object, byval e as
MouseEventArgs) Handles txtOcr.MouseUp
'code to display a contextmenu at the current position
End SUb

with C#, you have multiple things to cut/paste: the contextmenu code, the
declaration, AND the event hookup, obviously with the VB code, the hookup is
handled already.

AGAIN, I'm not totally comfortable with the compiler emitting arbitrary
code, so I'm looking for a good solution that can bridge the gap, then to
eventually hound the compiler guys ;-)

--
Eric Newton
C#/ASP Application Developer
http://ensoft-software.com/
er**@cc.ensoft-software.com [remove the first "CC."]

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Eric Newton <er**@cc.ensoft-software.com> wrote:
VB's more declarative nature of handling events is golden. I'm hoping C# will acquire this type of deal, in addition to the anonymous delegates.

could do same as vb (actually would be easier to parse then vb due to
braces) whereas you look for Handles keyword after method sig, but before opening brace of the method.

compiler would implicitly build event handling code for all typed
constructions, ie, any variables instances constructed via "new" operator, compiler would follow with <instanceName>.<event> += new
<eventHandlerType>(<methodNameOfHandler>)

Seems like the algo could work like this

if "Handles" is found between method signature and first opening brace,
follow rules for dereferencing a simple expression of a type, followed by an event name.
if expression is valid then
build implied code event handler creation for all found "new" operators of the class
end if


<snip>

While I can see some of the appeal, this strikes me as a really nasty
thing to happen in terms of the language. It's just not the kind of
thing the language should be trying to deal with. It would also be open
to problems - for instance, what if your button wasn't generated by a
"new" operator but by a method returning a button reference?

I'm sure there's an elegant solution around somewhere, but I don't know
what it is. Here's another idea though:

// Variable declaration
eventhandled Button button1;

// Method declaration
private void Button_Click(object sender,EventArgs e) handles
Button1.Click

generates code equivalent to:

Button _button1;
Button button1
{
get { return button1; }
set
{
if (button1 != null)
{
button1.Click -= new EventHandler (Button_Click);
}
button1 = value;
if (button1 != null)
{
button1.Click += new EventHandler (Button_Click);
}
}
}

In other words, the field is turned into a property and code is
generated to add/remove the event handlers whenever the value is
changed.

Things would get very sticky when it came to inheritance though - the
base class would only know about events within the class itself, so a
derived class couldn't "add" event handlers. Maybe that would be a
reasonable restriction though.

I'm not sure I like disguising a property as a field, but I feel it's a
bit nicer (and more reliable) than other ways. Mind you, it still has
problems:

Button tmp = button1;
button1 = new Button(...);
// The original button no longer has event handling; is this okay or
// not?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3

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

Similar topics

0
by: kmcnet | last post by:
Hello Everyone and thanks for your help in advance. I am working on a project that requires the programming of the MS Fax Service. I need to be able to react to events, specifically and incoming...
3
by: Ashok Kumar K | last post by:
Hi all, Where can I get some insight on using the __hook, __unhook, event_source and event_receiver for specifically COM events. The documentation given in MSDN is very minimal. I have the...
2
by: John Dann | last post by:
This question has arisen from an earlier thread but is really a separate issue: I have a VB.Net listbox control on a form. I want to be able to do 2 things with items displayed within the one...
1
by: JeffDotNet | last post by:
I have been enjoying the declarative use of the new sqlDataSource in asp2.0. It makes paging and query building extremely quick and simple. However occasionally I expect to get a timeout...
2
by: Jordan | last post by:
I need to handle UI events in a worker thread instead of the primary UI thread. In C#, is the normal UI event handling behavior to run in a context thread on the thread pool or are events always...
9
by: CuriousGeorge | last post by:
Can someone explain why this code DOES NOT raise a null reference exception? //////////////////////////// Program.cs ///////////////////////////////////////////// using System; using...
2
by: Kevin Frey | last post by:
One of my chief criticisms of validators in an ASP.NET page is that they can result in a developer re-implementing much of the "business logic" of a transaction at the page level. Assuming we...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
3
by: Nelson | last post by:
Hi All, I want to look at what happens to stock prices after a certain number of consecutive up or down days. Let's say, for instance, I'd like to see where a stock's price is 5 days after a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.