473,558 Members | 2,997 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
<eventHandlerTy pe>(<methodName OfHandler>)

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(ob ject sender,EventArg s e) handles Button1.Click,
Button2.Click, Button3.Click
{
}
protected Button Button1;
private void InitializeCompo nent()
{
Button1 = new Button();
// implicit compiler event handling addition
Button1.Click += new EventHandler(Bu tton1_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(Bu tton_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 2090
Eric Newton <er**@cc.enso ft-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
<eventHandlerTy pe>(<methodName OfHandler>)

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(ob ject sender,EventArg s 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.co m>
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 InitializeCompo nent 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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Eric Newton <er**@cc.enso ft-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
<eventHandlerTy pe>(<methodName OfHandler>)

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(ob ject sender,EventArg s 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.co m>
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
1714
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 fax. I have looked at the documentation for programming and there are some VB6 examples, such as: ...
3
2066
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 following scenario Server (COM event source) is written in VC++ 6.0 using ATL (event interfaces can be either dispinterface / IDispatch based) Client...
2
2661
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 listbox. 1. Reorder items using drag and drop with the mouse. 2. Delete items eg by double-clicking or some other mouse-related action.
1
1675
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 exception. Without filling the sqlDataSource manually I don’t know how to go about catching an exception. (Most likely but not limited to a timeout ...
2
2816
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 invoked on the primary UI thread? Thanks, Jordan
9
1823
by: CuriousGeorge | last post by:
Can someone explain why this code DOES NOT raise a null reference exception? //////////////////////////// Program.cs ///////////////////////////////////////////// using System; using System.Collections.Generic; using System.Text; using CSharpLib;
2
1752
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 have an object that implements business logic, and that object is utilised via an ObjectDataSource, I wish to enquire what the correct method is for...
132
5506
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 hard-to-find bugs. http://distributed-software.blogspot.com/2007/01/c-exception-handling-is-defective.html Regards, zorabi@ZHMicro.com...
3
1729
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 run of 5 consecutive up days. I can't even begin to figure out the queries needed to accomplish this. Maybe somebody can help me.
0
7629
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...
0
7835
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. ...
0
8061
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...
1
7593
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...
0
7914
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6183
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...
0
3578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1164
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
869
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...

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.