Adding delegates via +=

E

Edward Diener

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::SomeObjectMethod); // 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); }
 
D

Daniel O'Connell

Edward Diener said:
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::SomeObjectMethod); // 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/d...tml/frlrfsystemdelegateclasscombinetopic2.asp
 
E

Edward Diener

Daniel said:
Edward Diener said:
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::SomeObjectMethod); //
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.
 
D

Daniel O'Connell

Edward Diener said:
Daniel said:
Edward Diener said:
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::SomeObjectMethod); //
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.
 
T

Tomas Restrepo \(MVP\)

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...)
 

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

Top