473,769 Members | 6,337 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help understanding delegates?

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

namespace Something.Somet hing1
{
using System;

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

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

public void Log2 (int l)
{
Console.WriteLi ne("#{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, onIEDocComplete 1 fires. After the second
page load completes, onIEDocComplete 2 fires. After the third page load
completes, onIEDocComplete 3 fires.

IE_Inst.Documen tComplete += new
SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e1);

IE_Inst.Documen tComplete += new
SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e2);

IE_Inst.Documen tComplete += new
SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e3);

Thanks,
Brett
Nov 17 '05 #1
29 1892
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.o rg> wrote in message
news:uf******** ******@TK2MSFTN GP14.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 onIEDocComplete 1 handler and add the
onIEDocComplete 2 one when onIEDocComplete 1 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.o rg> wrote in message
news:OQ******** ******@TK2MSFTN GP14.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 onIEDocComplete 1 handler and add the
onIEDocComplete 2 one when onIEDocComplete 1 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.o rg> wrote in message
news:OQ******** ******@TK2MSFTN GP14.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 onIEDocComplete 1 handler and add the
onIEDocComplete 2 one when onIEDocComplete 1 is called.

Mattias


How exactly do I remove the IE delegate?

I try:

IE_Inst.Documen tComplete -=
SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e);

Compiler says:
Method 'appname.IE.onI EDocComplete(ob ject, ref object)' referenced without
parentheses

So I try
IE_Inst.Documen tComplete -=
SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e());

Compiler says:
No overload for method 'onIEDocComplet e' 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.Docume ntComplete -=
SHDocVw.DWebBr owserEvents2_Do cumentCompleteE ventHandler(thi s.onIEDocComple te);

Compiler says:
Method 'appname.IE.onI EDocComplete(ob ject, ref object)' referenced without
parentheses


Insert the new keyword before the event type.

IE_Inst.Documen tComplete -= new
SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e);

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.o rg> 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.Combin e for a few more details.

--
Jon Skeet - <sk***@pobox.co m>
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.co m> 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.Combin e 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.co m>
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******** ********@TK2MSF TNGP10.phx.gbl. ..

"Mattias Sjögren" <ma************ ********@mvps.o rg> wrote in message
news:OQ******** ******@TK2MSFTN GP14.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 onIEDocComplete 1 handler and add the
onIEDocComplete 2 one when onIEDocComplete 1 is called.

Mattias


How exactly do I remove the IE delegate?

I try:

IE_Inst.Documen tComplete -=
SHDocVw.DWebBro wserEvents2_Doc umentCompleteEv entHandler(this .onIEDocComplet e);

You are missing the "new"

IE_Inst.Documen tComplete -= 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

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

Similar topics

12
2953
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 starting is that it has a welcome window first which gets displayed and then the real windows after a while,in other words it means that the process name is the same, but the handle I need to retrive is the one from the final window and not the...
22
4070
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
1876
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 link to a site that describes delegates using VB.Net in a manner that doesn't require rocket science to become enlightened. TIA
2
1093
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 someone please help me understand why i would use a delegate in day to day use, over a function or sub? Thank you for your time! Ron
0
1093
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 worried they are going to make me do my own delegates from scratch.) I see that line 33 of Default.aspx is indeed executing when I single step with the debugger. As you can see (on lines of 17 and 18 of propertyDemo.cs I have set up a delegate...
13
1971
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
1949
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 Windows Callback is to generalise our API so that it can be compatible with any other language e.g. C++. Using normal delegates will not help us since delegates is a VB.Net feature and any other language cant make a use of it. Could please
4
1298
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 to a method or function or whatever... So basically you create an object that references the address of the meathod. By calling this delegate - you get a 'return' right away so your code can continue and the delegate runs in a different...
2
10045
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 is used to access and read the value stored in it C.A variable is allocated or deallocated in memory during runtime D.A variable can be initialized at the time of its creation or later 2. The.……types feature facilitates the definition of classes...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10050
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9999
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.