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

Adding delegates via +=

Does the += notation for adding delegates to a delegate pointer object work
even if the delegate pointer is 0 ? As an example:

__gc class SomeObjectClass { public: void SomeObjectMethod() { } };
SomeObjectClass * someObjectPointer(new SomeObjectClass);
__delegate void MyDelegate();
MyDelegate * dobject(0);
dobject += new
MyDelegate(someObjectPointer,&SomeObjectClass::Som eObjectMethod); // Is this
valid ?

or does one have to check for the 0 pointer and write:

if (dobject) { dobject += new MyDelegate(someObjectPointer
*,&SomeObjectClass::SomeObjectMethod); }
else { dobject = new MyDelegate(someObjectPointer
*,&SomeObjectClass::SomeObjectMethod); }
Nov 17 '05 #1
4 1260

"Edward Diener" <ed******@tropicsoft.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Does the += notation for adding delegates to a delegate pointer object work even if the delegate pointer is 0 ? As an example:

__gc class SomeObjectClass { public: void SomeObjectMethod() { } };
SomeObjectClass * someObjectPointer(new SomeObjectClass);
__delegate void MyDelegate();
MyDelegate * dobject(0);
dobject += new
MyDelegate(someObjectPointer,&SomeObjectClass::Som eObjectMethod); // Is this valid ?

or does one have to check for the 0 pointer and write:

if (dobject) { dobject += new MyDelegate(someObjectPointer
*,&SomeObjectClass::SomeObjectMethod); }
else { dobject = new MyDelegate(someObjectPointer
*,&SomeObjectClass::SomeObjectMethod); }
The += calls System::Delegate::Combine (IL disassembly from a test class):
call class [mscorlib]System.Delegate
[mscorlib]System.Delegate::Combine(class [mscorlib]System.Delegate,class
[mscorlib]System.Delegate)

The System::Delegate::Combine method will take nulls properly, as outlined
in msdn[1]:
A new multicast (combinable) delegate with an invocation list that
concatenates the invocation lists of a and b in that order.

-or-

a, if b is a null reference (Nothing in Visual Basic).

-or-

b, if a is a null reference (Nothing).

-or-

A null reference (Nothing), if both a and b are a null reference (Nothing).

1.
http://msdn.microsoft.com/library/de...binetopic2.asp

Nov 17 '05 #2
Daniel O'Connell wrote:
"Edward Diener" <ed******@tropicsoft.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Does the += notation for adding delegates to a delegate pointer
object work even if the delegate pointer is 0 ? As an example:

__gc class SomeObjectClass { public: void SomeObjectMethod() { } };
SomeObjectClass * someObjectPointer(new SomeObjectClass);
__delegate void MyDelegate();
MyDelegate * dobject(0);
dobject += new
MyDelegate(someObjectPointer,&SomeObjectClass::Som eObjectMethod); //
Is this valid ?

or does one have to check for the 0 pointer and write:

if (dobject) { dobject += new MyDelegate(someObjectPointer
*,&SomeObjectClass::SomeObjectMethod); }
else { dobject = new MyDelegate(someObjectPointer
*,&SomeObjectClass::SomeObjectMethod); }


The += calls System::Delegate::Combine (IL disassembly from a test
class): call class [mscorlib]System.Delegate
[mscorlib]System.Delegate::Combine(class
[mscorlib]System.Delegate,class [mscorlib]System.Delegate)


I have got to learn to use the IL disassembly and look and understand IL
code <g> .

I was looking in the documentation for System::MulticastDelegate but I
couldn't find any static operators. That's odd, but maybe the MSDN doc just
doesn't show any operators for MulticastDelegate. If += calls Combine I do
see where nulls are handled correctly in the Delegate::Combine doc. I assume
that -= calls Delegate::Remove. I do think these static operators should be
documented somewhere. Thanks for the information.
Nov 17 '05 #3

"Edward Diener" <ed******@tropicsoft.com> wrote in message
news:ea**************@TK2MSFTNGP12.phx.gbl...
Daniel O'Connell wrote:
"Edward Diener" <ed******@tropicsoft.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Does the += notation for adding delegates to a delegate pointer
object work even if the delegate pointer is 0 ? As an example:

__gc class SomeObjectClass { public: void SomeObjectMethod() { } };
SomeObjectClass * someObjectPointer(new SomeObjectClass);
__delegate void MyDelegate();
MyDelegate * dobject(0);
dobject += new
MyDelegate(someObjectPointer,&SomeObjectClass::Som eObjectMethod); //
Is this valid ?

or does one have to check for the 0 pointer and write:

if (dobject) { dobject += new MyDelegate(someObjectPointer
*,&SomeObjectClass::SomeObjectMethod); }
else { dobject = new MyDelegate(someObjectPointer
*,&SomeObjectClass::SomeObjectMethod); }
The += calls System::Delegate::Combine (IL disassembly from a test
class): call class [mscorlib]System.Delegate
[mscorlib]System.Delegate::Combine(class
[mscorlib]System.Delegate,class [mscorlib]System.Delegate)


I have got to learn to use the IL disassembly and look and understand IL
code <g> .

I was looking in the documentation for System::MulticastDelegate but I
couldn't find any static operators. That's odd, but maybe the MSDN doc

just doesn't show any operators for MulticastDelegate. If += calls Combine I do
see where nulls are handled correctly in the Delegate::Combine doc. I assume that -= calls Delegate::Remove. I do think these static operators should be documented somewhere. Thanks for the information. Yeap, -= calls Delegate::Remove. They are documented under Delegate.Combine
and Delegate.Remove, I am surprised they are not under MulticastDelegate,
but due to strange designs in the system, MulticastDelegate really shouldn't
exist(there are no non-multicast delegates, just multicast with one target),
and it may well be an error in the docs.

Nov 17 '05 #4
Hi Edward,
I was looking in the documentation for System::MulticastDelegate but I
couldn't find any static operators. That's odd, but maybe the MSDN doc just doesn't show any operators for MulticastDelegate.
Actually, += and -= don't really exist on delegates; they are purely
syntactic sugar performed by the compilers, which, as you can see, generate
calls to Delegate::Combine() and Delegate::Remove() as appropriate.
If += calls Combine I do
see where nulls are handled correctly in the Delegate::Combine doc. I assume that -= calls Delegate::Remove. I do think these static operators should be documented somewhere. Thanks for the information.


They are, just don't look for non-existant operators ;)
That said, since they are language-level features, they should be documented
on the language specification (which they aren't, as far as I can see...)

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #5

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

Similar topics

6
by: Jeffrey T. Smith | last post by:
Back when the new J2SE1.5 features were announced, there was a JavaLive community chat (http://java.sun.com/developer/community/chat/JavaLive/2003/jl0729.html) in which Neal Gafter explains the...
5
by: Ian Richardson | last post by:
I'm writing some code which does one thing when onreadystatechange occurs, e.g. handle.onreadystatechange = function() { blah(handle,other_params) }; ....but sometimes I need to add another,...
3
by: Sam | last post by:
I’m just starting to learn delegates. I’m at the very beginning. If I understand correctly, delegates are for when you want to pass a function as a parameter. For example the client provides a...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
1
by: Deckarep | last post by:
Dear CSharp Group, Both of these techniques work as expected but what is the better way of doing this? Or does it even make a difference? Method 1: public delegate void MyDelegate(string...
6
by: =?Utf-8?B?Sko=?= | last post by:
I have a logger component that logs to multiple sources, ie textfile, eventlog etc. and I have two methods that depending on where I call up my logger comp. one of them will be called. For ex. if...
0
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
6
by: =?Utf-8?B?T2xkQ2FEb2c=?= | last post by:
My question is regarding the use of delegates in C#. I see how .Net uses delegates to wire event handlers to events. It’s an object created by a single line of code by the system and that makes...
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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...
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: 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...

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.