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

Help understanding delegates?

I'd like to better understand how the following code works. I've posted
questions below.

namespace Something.Something1
{
using System;

public delegate void Test1();
public delegate void Test2(ink k);

public class A
{
public static void Log1 (int l)
{
Console.WriteLine(":{0}",l);
}

public void Log2 (int l)
{
Console.WriteLine("#{0}",l);
}
}

public class Class1
{
public static void Main()
{
A a = new A();
Test2 t0 = new Test2(A.Log1); //use static method
Test2 t1 = new Test2(a.Log2); //use instance method

t0 += t1; t0(0);
t0 -= t1; t0(1);
}
}
}

This will print ":0 #0 :1". How does Log1 print 0 when no param is passed
to it and "l" is not initialized to 0?

What are the differences in the static and instance method calls via
delegates?

This line t0 -= t1; t0(1); removes those methods from the delegate
collection correct? How is the output affected if that line does not
exists?

Also, if I'm using the IE object and have three successive page loads, will
each of the following events respectively fire after each page load rather
than all three always firing after each page load? In other words, after
the first page load completes, onIEDocComplete1 fires. After the second
page load completes, onIEDocComplete2 fires. After the third page load
completes, onIEDocComplete3 fires.

IE_Inst.DocumentComplete += new
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocComplete1);

IE_Inst.DocumentComplete += new
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocComplete2);

IE_Inst.DocumentComplete += new
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocComplete3);

Thanks,
Brett
Nov 17 '05 #1
29 1855
How does Log1 print 0 when no param is passed
to it and "l" is not initialized to 0?
What do you mean no param is passed to it? You're passing 0 when
invoking the delegate so that's the value l will get.

What are the differences in the static and instance method calls via
delegates?
No differences apart from the ones seen in regular method calls.

This line t0 -= t1; t0(1); removes those methods from the delegate
collection correct?
It removes the t1 methods (Log2) from t0, but leaves Log1.

How is the output affected if that line does not exists?


Why don't you try it and see.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:uf**************@TK2MSFTNGP14.phx.gbl...
How does Log1 print 0 when no param is passed
to it and "l" is not initialized to 0?


What do you mean no param is passed to it? You're passing 0 when
invoking the delegate so that's the value l will get.


I mean to say t1 or Log2. Zero is not passed it yet it is displayed.

Any suggestions on the IE question?
Nov 17 '05 #3
>>>How does Log1 print 0 when no param is passed
to it and "l" is not initialized to 0?
What do you mean no param is passed to it? You're passing 0 when
invoking the delegate so that's the value l will get.


I mean to say t1 or Log2. Zero is not passed it yet it is displayed.


Log2 is called with the argument 0 when you invoke the t0 delegate
because Log2 is in t0's list of methods to call after you do t0 += t1.

Any suggestions on the IE question?

Also, if I'm using the IE object and have three successive page loads, will
each of the following events respectively fire after each page load rather
than all three always firing after each page load?


No they will all fire for every load event. If that's not what you
want, you'll have to remove the onIEDocComplete1 handler and add the
onIEDocComplete2 one when onIEDocComplete1 is called.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #4

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:OQ**************@TK2MSFTNGP14.phx.gbl...
How does Log1 print 0 when no param is passed
to it and "l" is not initialized to 0?

What do you mean no param is passed to it? You're passing 0 when
invoking the delegate so that's the value l will get.


I mean to say t1 or Log2. Zero is not passed it yet it is displayed.


Log2 is called with the argument 0 when you invoke the t0 delegate
because Log2 is in t0's list of methods to call after you do t0 += t1.

Any suggestions on the IE question?

Also, if I'm using the IE object and have three successive page loads,
will
each of the following events respectively fire after each page load
rather
than all three always firing after each page load?


No they will all fire for every load event. If that's not what you
want, you'll have to remove the onIEDocComplete1 handler and add the
onIEDocComplete2 one when onIEDocComplete1 is called.


So is a delegate basically a collection of method pointers that when called,
executes all methods in the collection in the order they were added to the
collection?

Brett
Nov 17 '05 #5

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:OQ**************@TK2MSFTNGP14.phx.gbl...
How does Log1 print 0 when no param is passed
to it and "l" is not initialized to 0?

What do you mean no param is passed to it? You're passing 0 when
invoking the delegate so that's the value l will get.


I mean to say t1 or Log2. Zero is not passed it yet it is displayed.


Log2 is called with the argument 0 when you invoke the t0 delegate
because Log2 is in t0's list of methods to call after you do t0 += t1.

Any suggestions on the IE question?

Also, if I'm using the IE object and have three successive page loads,
will
each of the following events respectively fire after each page load
rather
than all three always firing after each page load?


No they will all fire for every load event. If that's not what you
want, you'll have to remove the onIEDocComplete1 handler and add the
onIEDocComplete2 one when onIEDocComplete1 is called.

Mattias


How exactly do I remove the IE delegate?

I try:

IE_Inst.DocumentComplete -=
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocComplete);

Compiler says:
Method 'appname.IE.onIEDocComplete(object, ref object)' referenced without
parentheses

So I try
IE_Inst.DocumentComplete -=
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocComplete());

Compiler says:
No overload for method 'onIEDocComplete' takes '0' arguments

Brett
Nov 17 '05 #6
So is a delegate basically a collection of method pointers that when called,
executes all methods in the collection in the order they were added to the
collection?
Correct, except that I believe the order they are called in is
undefined and implementation dependent.

How exactly do I remove the IE delegate?

I try:

IE_Inst.DocumentComplete -=
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventH andler(this.onIEDocComplete);

Compiler says:
Method 'appname.IE.onIEDocComplete(object, ref object)' referenced without
parentheses


Insert the new keyword before the event type.

IE_Inst.DocumentComplete -= new
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocComplete);

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #7
Mattias Sjögren <ma********************@mvps.org> wrote:
So is a delegate basically a collection of method pointers that when called,
executes all methods in the collection in the order they were added to the
collection?


Correct, except that I believe the order they are called in is
undefined and implementation dependent.


Nope - the order is guaranteed. Have a look at the docs for
Delegate.Combine for a few more details.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Correct, except that I believe the order they are called in is
undefined and implementation dependent.


Nope - the order is guaranteed. Have a look at the docs for
Delegate.Combine for a few more details.


Sorry, looking further, I can't see anything that absolutely guarantees
that when you call Invoke, the delegates are executed in the
appropriate order. However, things like the C# specification assume
that that's going to happen, and I think it's a pretty reasonable
assumption to make.

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

"Brett" <no@spam.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:OQ**************@TK2MSFTNGP14.phx.gbl...
>How does Log1 print 0 when no param is passed
>to it and "l" is not initialized to 0?

What do you mean no param is passed to it? You're passing 0 when
invoking the delegate so that's the value l will get.

I mean to say t1 or Log2. Zero is not passed it yet it is displayed.


Log2 is called with the argument 0 when you invoke the t0 delegate
because Log2 is in t0's list of methods to call after you do t0 += t1.

Any suggestions on the IE question?

Also, if I'm using the IE object and have three successive page loads,
will
each of the following events respectively fire after each page load
rather
than all three always firing after each page load?


No they will all fire for every load event. If that's not what you
want, you'll have to remove the onIEDocComplete1 handler and add the
onIEDocComplete2 one when onIEDocComplete1 is called.

Mattias


How exactly do I remove the IE delegate?

I try:

IE_Inst.DocumentComplete -=
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocComplete);

You are missing the "new"

IE_Inst.DocumentComplete -= new ....

Not sure why you don't take some time to read the Events tutorial in MSDN's
"C# Programmers Reference"

Willy.


Nov 17 '05 #10
-
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:OO*************@TK2MSFTNGP15.phx.gbl...

"Brett" <no@spam.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:OQ**************@TK2MSFTNGP14.phx.gbl...
>>How does Log1 print 0 when no param is passed
>>to it and "l" is not initialized to 0?
>
> What do you mean no param is passed to it? You're passing 0 when
> invoking the delegate so that's the value l will get.

I mean to say t1 or Log2. Zero is not passed it yet it is displayed.

Log2 is called with the argument 0 when you invoke the t0 delegate
because Log2 is in t0's list of methods to call after you do t0 += t1.
Any suggestions on the IE question?

>Also, if I'm using the IE object and have three successive page loads,
>will
>each of the following events respectively fire after each page load
>rather
>than all three always firing after each page load?

No they will all fire for every load event. If that's not what you
want, you'll have to remove the onIEDocComplete1 handler and add the
onIEDocComplete2 one when onIEDocComplete1 is called.

Mattias


How exactly do I remove the IE delegate?

I try:

IE_Inst.DocumentComplete -=
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocComplete);

You are missing the "new"

IE_Inst.DocumentComplete -= new ....

Not sure why you don't take some time to read the Events tutorial in
MSDN's "C# Programmers Reference"

Willy.


Because it's not so clear how the particular thing with IE OnLoad is done.
You get much better feedback here anyway.

Brett
Nov 17 '05 #11

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
> Correct, except that I believe the order they are called in is
> undefined and implementation dependent.


Nope - the order is guaranteed. Have a look at the docs for
Delegate.Combine for a few more details.


Sorry, looking further, I can't see anything that absolutely guarantees
that when you call Invoke, the delegates are executed in the
appropriate order. However, things like the C# specification assume
that that's going to happen, and I think it's a pretty reasonable
assumption to make.

I thought C# collections didn't have order?
Nov 17 '05 #12

"Brett" <no@spam.net> wrote in message
news:OM**************@TK2MSFTNGP12.phx.gbl...
-
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:OO*************@TK2MSFTNGP15.phx.gbl...

"Brett" <no@spam.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:OQ**************@TK2MSFTNGP14.phx.gbl...
>>>How does Log1 print 0 when no param is passed
>>>to it and "l" is not initialized to 0?
>>
>> What do you mean no param is passed to it? You're passing 0 when
>> invoking the delegate so that's the value l will get.
>
>I mean to say t1 or Log2. Zero is not passed it yet it is displayed.

Log2 is called with the argument 0 when you invoke the t0 delegate
because Log2 is in t0's list of methods to call after you do t0 += t1.
>Any suggestions on the IE question?

>>Also, if I'm using the IE object and have three successive page loads,
>>will
>>each of the following events respectively fire after each page load
>>rather
>>than all three always firing after each page load?

No they will all fire for every load event. If that's not what you
want, you'll have to remove the onIEDocComplete1 handler and add the
onIEDocComplete2 one when onIEDocComplete1 is called.

Mattias

How exactly do I remove the IE delegate?

I try:

IE_Inst.DocumentComplete -=
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocComplete);

You are missing the "new"

IE_Inst.DocumentComplete -= new ....

Not sure why you don't take some time to read the Events tutorial in
MSDN's "C# Programmers Reference"

Willy.


Because it's not so clear how the particular thing with IE OnLoad is done.
You get much better feedback here anyway.

Brett


If you did consult the documentation first, it wouldn't (maybe) be necessary
to ask questions like this one and you wouldn't ask about delegates while in
fact you were asking about Events.

Willy.

Nov 17 '05 #13

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:eM**************@TK2MSFTNGP15.phx.gbl...

"Brett" <no@spam.net> wrote in message
news:OM**************@TK2MSFTNGP12.phx.gbl...
-
"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:OO*************@TK2MSFTNGP15.phx.gbl...

"Brett" <no@spam.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:OQ**************@TK2MSFTNGP14.phx.gbl...
>>>>How does Log1 print 0 when no param is passed
>>>>to it and "l" is not initialized to 0?
>>>
>>> What do you mean no param is passed to it? You're passing 0 when
>>> invoking the delegate so that's the value l will get.
>>
>>I mean to say t1 or Log2. Zero is not passed it yet it is displayed.
>
> Log2 is called with the argument 0 when you invoke the t0 delegate
> because Log2 is in t0's list of methods to call after you do t0 += t1.
>
>
>>Any suggestions on the IE question?
>
>>>Also, if I'm using the IE object and have three successive page
>>>loads, will
>>>each of the following events respectively fire after each page load
>>>rather
>>>than all three always firing after each page load?
>
> No they will all fire for every load event. If that's not what you
> want, you'll have to remove the onIEDocComplete1 handler and add the
> onIEDocComplete2 one when onIEDocComplete1 is called.
>
>
>
> Mattias

How exactly do I remove the IE delegate?

I try:

IE_Inst.DocumentComplete -=
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocComplete);

You are missing the "new"

IE_Inst.DocumentComplete -= new ....

Not sure why you don't take some time to read the Events tutorial in
MSDN's "C# Programmers Reference"

Willy.


Because it's not so clear how the particular thing with IE OnLoad is
done. You get much better feedback here anyway.

Brett


If you did consult the documentation first, it wouldn't (maybe) be
necessary to ask questions like this one and you wouldn't ask about
delegates while in fact you were asking about Events.

Willy.


Delegates and events are related. It's not uncommon that asking about one
brings up a question on the other...if you are a beginner.

Brett
Nov 17 '05 #14
Brett <no@spam.net> wrote:
Nope - the order is guaranteed. Have a look at the docs for
Delegate.Combine for a few more details.


Sorry, looking further, I can't see anything that absolutely guarantees
that when you call Invoke, the delegates are executed in the
appropriate order. However, things like the C# specification assume
that that's going to happen, and I think it's a pretty reasonable
assumption to make.

I thought C# collections didn't have order?


That entirely depends on the collection - and delegates aren't
collections in the normal sense anyway.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #15
"..delegates aren't collections in the normal sense anyway."

What do you mean by that?
Nov 17 '05 #16
Brett <no@spam.com> wrote:
"..delegates aren't collections in the normal sense anyway."

What do you mean by that?


Well, they don't implement ICollection, to start with.

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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Brett <no@spam.com> wrote:
"..delegates aren't collections in the normal sense anyway."

What do you mean by that?


Well, they don't implement ICollection, to start with.


What benefits are there to using ICollection in this case? Isn't this still
a collection either way?

Brett
Nov 17 '05 #18
Brett <no@spam.net> wrote:
What do you mean by that?


Well, they don't implement ICollection, to start with.


What benefits are there to using ICollection in this case? Isn't this still
a collection either way?


It's no a collection in any normal sense of being able to manipulate
it. Yes, there's an invocation list associated with the event, but you
can only add to it (at either end) or remove from it, both of which
create a new list rather than changing the existing one.

If you're going to use that broad a definition of "collection", how
could you possibly say that collections in C#/.NET are unordered? An
array would count as a collection, and that *certainly* isn't
unordered. *Some* collections (usually maps) are unordered, but lists
are generally ordered.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #19
Brett <no@spam.com> wrote:
What do you mean by "get away with using a collection"?
Perhaps it isn't a collection but just a list, as you have mentioned.


The delegate itself isn't a list - it encapsulates a list of
invocations, but it isn't a list in itself.

When you say that you "still get away with using a collection in this
case" what exactly do you mean?
Probably just do a GetType() to determine it.


I don't see how that would help. Could you give an example of what you
mean?

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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Brett <no@spam.com> wrote:
> What do you mean by "get away with using a collection"?


Perhaps it isn't a collection but just a list, as you have mentioned.


The delegate itself isn't a list - it encapsulates a list of
invocations, but it isn't a list in itself.

When you say that you "still get away with using a collection in this
case" what exactly do you mean?
Probably just do a GetType() to determine it.


I don't see how that would help. Could you give an example of what you
mean?


I don't have an example. Was just trying to get a solid definition of what
this structure is that holds a list of events. First it was something that
behaved like a collection, then it was a list. Then not a list but
something encapsulating a list. One of us is very confused but I'm backing
off now. ; )

Thanks,
Brett
Nov 17 '05 #21
Brett <no@spam.com> wrote:
I don't see how that would help. Could you give an example of what you
mean?


I don't have an example. Was just trying to get a solid definition of what
this structure is that holds a list of events. First it was something that
behaved like a collection, then it was a list. Then not a list but
something encapsulating a list. One of us is very confused but I'm backing
off now. ; )


Basically, it contains a list. It's immutable, so you can't change the
list itself, only create a new delegate with a different list
containing the first list. You can't use it as a general purpose
collection, as it can only hold delegates.

The things you've said that confuse me most are:

1) That collections in C# aren't ordered
2) That you've used delegates as collections with no trouble

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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Brett <no@spam.com> wrote:
> I don't see how that would help. Could you give an example of what you
> mean?
I don't have an example. Was just trying to get a solid definition of
what
this structure is that holds a list of events. First it was something
that
behaved like a collection, then it was a list. Then not a list but
something encapsulating a list. One of us is very confused but I'm
backing
off now. ; )


Basically, it contains a list. It's immutable, so you can't change the
list itself, only create a new delegate with a different list
containing the first list. You can't use it as a general purpose
collection, as it can only hold delegates.

The things you've said that confuse me most are:

1) That collections in C# aren't ordered

My fault.
2) That you've used delegates as collections with no trouble I thought they were collections. I've read they were in some of the online
examples I've seen. That's how they are described. That's not a global
description of course. Perhaps this exchange with Mattias further up in
the thread did me in. You see it hits both of your questions.
[>So is a delegate basically a collection of method pointers that when
called,executes all methods in the collection in the order they were added to the
collection?


Correct, except that I believe the order they are called in is
undefined and implementation dependent.]
Nov 17 '05 #23
In message <#5**************@TK2MSFTNGP10.phx.gbl>, Brett <no@spam.com>
writes

2) That you've used delegates as collections with no trouble

I thought they were collections. I've read they were in some of the online
examples I've seen. That's how they are described. That's not a global
description of course.


I think this is just a case of terminology. It's quite common to use
"collection" to describe any container which can manage a number of
contained objects. I think Jon is using it in a stricter .NET specific
sense to mean "implements ICollection". I've got lots of classes
implementing IEnumerable which I would casually describe as
"collections", but by Jon's definition, they aren't.

--
Steve Walker
Nov 17 '05 #24

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Brett <no@spam.com> wrote:
> I don't see how that would help. Could you give an example of what you
> mean?


I don't have an example. Was just trying to get a solid definition of
what
this structure is that holds a list of events. First it was something
that
behaved like a collection, then it was a list. Then not a list but
something encapsulating a list. One of us is very confused but I'm
backing
off now. ; )


Basically, it contains a list. It's immutable, so you can't change the
list itself, only create a new delegate with a different list
containing the first list. You can't use it as a general purpose
collection, as it can only hold delegates.

The things you've said that confuse me most are:

1) That collections in C# aren't ordered
2) That you've used delegates as collections with no trouble

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


While we're on the subject,. how can I removed all events I've delegated?
For example:

IE_Inst.DocumentComplete -= new
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocCompleteGetMessages);

and at some later point clear out
IE_Inst.DocumentComplete

so that no event fires based on the page load complete.

Thanks,
Brett
Nov 17 '05 #25
Brett <no@spam.com> wrote:
While we're on the subject,. how can I removed all events I've delegated?
For example:

IE_Inst.DocumentComplete -= new
SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHa ndler(this.onIEDocCompleteGetMessages);

and at some later point clear out
IE_Inst.DocumentComplete

so that no event fires based on the page load complete.


You can only remove specific handlers - so if you know what you've
added, you can remove them again - but you can't just clear out
everything.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #26
mdb
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
You can only remove specific handlers - so if you know what you've
added, you can remove them again - but you can't just clear out
everything.


I think you would be able to iterate through the list of delegates (with
GetInvocationList) and remove each one. You probably can even tell whether
its something you've added (versus other classes) with the .Target and/or
..Method properties. Whether you can distinguish between two objects of the
same class with different events might be a bit more difficult, though.

-mdb
Nov 17 '05 #27
mdb <m_b_r_a_y@c_t_i_u_s_a__d0t__com> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
You can only remove specific handlers - so if you know what you've
added, you can remove them again - but you can't just clear out
everything.


I think you would be able to iterate through the list of delegates (with
GetInvocationList) and remove each one. You probably can even tell whether
its something you've added (versus other classes) with the .Target and/or
.Method properties. Whether you can distinguish between two objects of the
same class with different events might be a bit more difficult, though.


You can do that if you have access to the delegate - but if you only
have access to the *event*, you can't get the invocation list, only add
to it and remove from it.

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

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
mdb <m_b_r_a_y@c_t_i_u_s_a__d0t__com> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
> You can only remove specific handlers - so if you know what you've
> added, you can remove them again - but you can't just clear out
> everything.


I think you would be able to iterate through the list of delegates (with
GetInvocationList) and remove each one. You probably can even tell
whether
its something you've added (versus other classes) with the .Target and/or
.Method properties. Whether you can distinguish between two objects of
the
same class with different events might be a bit more difficult, though.


You can do that if you have access to the delegate - but if you only
have access to the *event*, you can't get the invocation list, only add
to it and remove from it.


Using the example in my initial post, say I add two events to the deletgate
t0. Can I use a foreach loop to get at items stored in the t0 delegate
type?

Now, it isn't necessary that I implement IEnurmerable to use a for each
loop? I ask that question because every time I have used a for each loop, I
haven't implemented IEnumerable. However, from the docs:
[IEnumerable must be implemented to support the ForEach semantics of
Microsoft Visual Basic. COM classes that allow enumerators also implement
this interface.]

If the above is true, then t0 is a collection because IEnumerable is in the
System.Collections namespace.

Brett
Nov 17 '05 #29
"Brett" <no@spam.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message [Snip]
You can do that if you have access to the delegate - but if you only
have access to the *event*, you can't get the invocation list, only add
to it and remove from it.


Using the example in my initial post, say I add two events to the
deletgate t0. Can I use a foreach loop to get at items stored in the t0
delegate type?


IF you have access to the delegate object itself you can foreach through
Delegate.GetInvocationList which is an array. Arrays implement IList which
inherits from IEnumerable via ICollection.
Now, it isn't necessary that I implement IEnurmerable to use a for each
loop? I ask that question because every time I have used a for each loop,
I haven't implemented IEnumerable. However, from the docs:
[IEnumerable must be implemented to support the ForEach semantics of
Microsoft Visual Basic. COM classes that allow enumerators also implement
this interface.]
The object you are enumerating on must implement IEnumerable (the one in the
"in" part of your foreach).
If the above is true, then t0 is a collection because IEnumerable is in
the System.Collections namespace.
Sorry, wrong. DictionaryEntry, Comparer, and IHashCodeProvider are all in
the System.Collections namespace and are not collections. Being in the
namespace does not imply that they ARE collections, merely that they are
related to collections in some way. You're confusing location with identity.
Just to reinforce my point, t0.GetInvocationList returns an array which IS a
collection (since it implements ICollection), but Array resides in the
System namespace.
Brett

Nov 17 '05 #30

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

Similar topics

12
by: serge calderara | last post by:
Dear all, I have an application which is suppose to start another executable process. As soon as that process is running, I need to retrive its handle. The problem of the particular process I am...
22
by: Jeff Louie | last post by:
Well I wonder if my old brain can handle threading. Dose this code look reasonable. Regards, Jeff using System; using System.Diagnostics; using System.IO; using System.Threading;
4
by: AMDRIT | last post by:
I am trying to understand Delegates and where/when to use them. I can see one potential use of a delegate (on form closing, set the cancel property in the event arguments.) Does anyone have a...
2
by: RSH | last post by:
I have been looking at delegates lately. I have seen several articles explaining them but I'm having a hard time understanding why I would use a delegate over a traditional function or sub. Could...
0
by: Siegfried Heintze | last post by:
I'm practicing for the C# brain bench test by reviewing how delegates work. (Delegates are easy to to with visual studio because you normally use the delegates that some API as already defined. I'm...
13
by: Praveen | last post by:
trying to learn plymorphism. My sample is public class Class1 { public static void Main(string args) { Cls1 x = new Cls1(); Cls2 y = new Cls2(); Cls3 y = new Cls3();
5
by: sajin | last post by:
Hi All.. We are using VB .Net 2005 for implementing an API. API needs to generate events. For this client wants us to use Windows Callback (delegate implementation). The intention of using...
4
by: Miro | last post by:
I am trying to understand delegates and I think I do understand them ... just hoping if someone can tell me im on the right track. So far what I have read is that a Delegate is an Asynchronus call...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
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: 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
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
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,...
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.