473,386 Members | 1,763 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.

Why do managed event handlers still use '&" ?

I've upgraded my code to VS C++.NET 2005 (Express) using /clr pure. My
question is, why is there an exception to the rule in how pointer syntax is
done in the event handlers?

For example, why is this proper syntax:

button->Click += gcnew EventHandler( this, &MyClass::MouseHandler ) ;

The '&' seems like a hold over from the old syntax, except this is the NEW
syntax (i.e., it never wanted the '&' before 2005 at all). Why was the '&'
added (it's not like anything BUT a pointer would make sense), and why was
the old syntax 'address of' symbol used? That is, before this was done like
this:

button->Click += new EventHandler( this, MyClass::MouseHandler ) ;

That is, no '&'. Thus, it seems the proper transition would be to this
syntax:

button->Click += gcew EventHandler( this, MyClass::MouseHandler ) ;

This means the only reason to add the '&' is if the line immediately above
could somehow be ambiguous, which I don't think it can be. So adding the
superfulous '&' seems odd.

Further, almost humerously, the compile upon seeing the non-'&' form
generates an error that basically says 'you need to add the '&'". Well, if
it knows this, and there is no ambiguity involved, why not accept BOTH
forms? Or dang it, why not provide the ability to click on such an error
message and have an option for IT make the correction? And possibly allow a
MAKE ALL CORRECTIONS button, letting the compiler do some of the work! Its
really a bit irritating when it says 'this is what you did wrong, this is
how its done' without the ability to fix it automatically (and such a method
would not be prone to typos!)...

[==P==]

Nov 28 '05 #1
4 2104
On Mon, 28 Nov 2005 14:22:07 -0800, "Peter Oliphant"
<po*******@RoundTripInc.com> wrote:
I've upgraded my code to VS C++.NET 2005 (Express) using /clr pure. My
question is, why is there an exception to the rule in how pointer syntax is
done in the event handlers?

For example, why is this proper syntax:

button->Click += gcnew EventHandler( this, &MyClass::MouseHandler ) ;

The '&' seems like a hold over from the old syntax, except this is the NEW
syntax (i.e., it never wanted the '&' before 2005 at all). Why was the '&'
added (it's not like anything BUT a pointer would make sense), and why was
the old syntax 'address of' symbol used? That is, before this was done like
this:

button->Click += new EventHandler( this, MyClass::MouseHandler ) ;

That is, no '&'. Thus, it seems the proper transition would be to this
syntax:

button->Click += gcew EventHandler( this, MyClass::MouseHandler ) ;

This means the only reason to add the '&' is if the line immediately above
could somehow be ambiguous, which I don't think it can be. So adding the
superfulous '&' seems odd.

Further, almost humerously, the compile upon seeing the non-'&' form
generates an error that basically says 'you need to add the '&'". Well, if
it knows this, and there is no ambiguity involved, why not accept BOTH
forms? Or dang it, why not provide the ability to click on such an error
message and have an option for IT make the correction? And possibly allow a
MAKE ALL CORRECTIONS button, letting the compiler do some of the work! Its
really a bit irritating when it says 'this is what you did wrong, this is
how its done' without the ability to fix it automatically (and such a method
would not be prone to typos!)...


Earlier versions of VC++ accepted just about any syntax to mean
pointer-to-member, which allowed people to make many subtle mistakes. For
example, how is the compiler to know in general that A::F isn't simply the
result of leaving off the parens on a function call? What about a->f? The
C++ Standard says the only way to form a pointer-to-member is with the
&A::F syntax, and that's a good thing.

They've actually fixed a real problem with delegates; it used to be
possible to create a C++ delegate from an unrelated object and
pointer-to-member, but I just translated my old VC7.1 example to C++/CLI,
and the compiler complains appropriately:

TDelegate^ d = gcnew TDelegate(gcnew X, &Y::f);

a.cpp(40) : error C3754: delegate constructor: member function 'Y::f'
cannot be called on an instance of type 'X ^'

In this program, X and Y are unrelated classes, but VC7.1 would allow the
equivalent of the above to pass, causing errors at runtime instead of
compile-time.

--
Doug Harrison
Visual C++ MVP
Nov 28 '05 #2
Hi Doug,

That brings up an interesting question. If it is illegal to create a handler
with unrelated objects (which totally makes sense), why is it required to
completely qualify the handler's name? That is, why do we have to do:

Click += gcnew EventHandler( this, &MyClass::Handler ) ;

why isn't this just fine:

Click += gcnew EventHandler( this, &Handler ) ;

[==P==]

"Doug Harrison [MVP]" <ds*@mvps.org> wrote in message
news:d4********************************@4ax.com...
On Mon, 28 Nov 2005 14:22:07 -0800, "Peter Oliphant"
<po*******@RoundTripInc.com> wrote:
I've upgraded my code to VS C++.NET 2005 (Express) using /clr pure. My
question is, why is there an exception to the rule in how pointer syntax
is
done in the event handlers?

For example, why is this proper syntax:

button->Click += gcnew EventHandler( this, &MyClass::MouseHandler ) ;

The '&' seems like a hold over from the old syntax, except this is the NEW
syntax (i.e., it never wanted the '&' before 2005 at all). Why was the '&'
added (it's not like anything BUT a pointer would make sense), and why was
the old syntax 'address of' symbol used? That is, before this was done
like
this:

button->Click += new EventHandler( this, MyClass::MouseHandler ) ;

That is, no '&'. Thus, it seems the proper transition would be to this
syntax:

button->Click += gcew EventHandler( this, MyClass::MouseHandler ) ;

This means the only reason to add the '&' is if the line immediately above
could somehow be ambiguous, which I don't think it can be. So adding the
superfulous '&' seems odd.

Further, almost humerously, the compile upon seeing the non-'&' form
generates an error that basically says 'you need to add the '&'". Well, if
it knows this, and there is no ambiguity involved, why not accept BOTH
forms? Or dang it, why not provide the ability to click on such an error
message and have an option for IT make the correction? And possibly allow
a
MAKE ALL CORRECTIONS button, letting the compiler do some of the work! Its
really a bit irritating when it says 'this is what you did wrong, this is
how its done' without the ability to fix it automatically (and such a
method
would not be prone to typos!)...


Earlier versions of VC++ accepted just about any syntax to mean
pointer-to-member, which allowed people to make many subtle mistakes. For
example, how is the compiler to know in general that A::F isn't simply the
result of leaving off the parens on a function call? What about a->f? The
C++ Standard says the only way to form a pointer-to-member is with the
&A::F syntax, and that's a good thing.

They've actually fixed a real problem with delegates; it used to be
possible to create a C++ delegate from an unrelated object and
pointer-to-member, but I just translated my old VC7.1 example to C++/CLI,
and the compiler complains appropriately:

TDelegate^ d = gcnew TDelegate(gcnew X, &Y::f);

a.cpp(40) : error C3754: delegate constructor: member function 'Y::f'
cannot be called on an instance of type 'X ^'

In this program, X and Y are unrelated classes, but VC7.1 would allow the
equivalent of the above to pass, causing errors at runtime instead of
compile-time.

--
Doug Harrison
Visual C++ MVP

Nov 29 '05 #3
"Peter Oliphant" <po*******@RoundTripInc.com> wrote in message
news:eg**************@TK2MSFTNGP14.phx.gbl...
Hi Doug,

That brings up an interesting question. If it is illegal to create a
handler with unrelated objects (which totally makes sense), why is it
required to completely qualify the handler's name? That is, why do we have
to do:

Click += gcnew EventHandler( this, &MyClass::Handler ) ;

why isn't this just fine:

Click += gcnew EventHandler( this, &Handler ) ;


Because the simple name 'Handler' is not in scope under normal C++ scoping
rules.

-cd
Nov 30 '05 #4
On Tue, 29 Nov 2005 15:36:42 -0800, "Peter Oliphant"
<po*******@RoundTripInc.com> wrote:
Hi Doug,

That brings up an interesting question. If it is illegal to create a handler
with unrelated objects (which totally makes sense), why is it required to
completely qualify the handler's name? That is, why do we have to do:

Click += gcnew EventHandler( this, &MyClass::Handler ) ;

why isn't this just fine:

Click += gcnew EventHandler( this, &Handler ) ;


It could do that, but it would make for a gratuitous incompatibility with
Standard C++. Remember, in Standard C++, there's exactly one way to form a
pointer to member. Also, qualifying the name with MyClass:: tells the
compiler exactly where in the class hierarchy to start looking for the name
"Handler". Normally, this would be the static type of *this, but it could
also be a base class. You might argue that since there's an obvious choice
of default starting point, the qualifier should be optional, but then we're
back to the "gratuitous incompatibility". I do get the feeling you'd like
the C# syntax better, which is, IIRC, something like
EventHandler(this.Handler).

--
Doug Harrison
Visual C++ MVP
Nov 30 '05 #5

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

Similar topics

2
by: DC Gringo | last post by:
I have an image control (that pulls an image off an ESRI map server): <ASP:IMAGE ID="imgZonedCountry" RUNAT="server"></ASP:IMAGE> In the code behind I am setting the ImageURL to a String value...
2
by: Eric Osman | last post by:
Hi, I'm looking for a javascript function that will convert input such as this: <CLUB Code=" into this: &lt;CLUB Code=&quot;
4
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following...
5
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot;...
7
by: DC Gringo | last post by:
I am having a bear of a time with setting a URL query string as a text value in a dropdownlist and Server.URLEncode does not seem to do its job. theFullLink = theLinkPrefix &...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
13
by: Ragnar | last post by:
Hi, 2 issues left with my tidy-work: 1) Tidy transforms a "&amp;" in the source-xml into a "&" in the tidied version. My XML-Importer cannot handle it 2) in a long <title>-string a wrap is...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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,...

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.