473,480 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

delegate problem

Hey

..NET 2.0

I'm trying to understand delegates. Below is an example I've created. I
don't understand how the delegate below becomes a reference pointer (AFIAK
delegates are reference pointers) to the method below it....

delegate decimal HelloWorldHandler(string name);
private decimal HelloWorld(string name)
{
}

any suggestions?
Jan 3 '08 #1
6 1246
Jeff <do***@spam.mewrote:
I'm trying to understand delegates. Below is an example I've created. I
don't understand how the delegate below becomes a reference pointer (AFIAK
delegates are reference pointers) to the method below it....

delegate decimal HelloWorldHandler(string name);
private decimal HelloWorld(string name)
{
}

any suggestions?
The first line defines a delegate *type*. You need a new *instance* of
the delegate, e.g.

HelloWorldHandler foo = HelloWorld;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jan 3 '08 #2
thanks, I understand that now

However there are something here I don't understand. I'm trying to
understand some .NET code using delegates. Below is another example which I
have trouble understanding. The example is taken from an app I'm programming
on (names on methods are changed):

delegate decimal HelloWorldHandler(string name);
private decimal HelloWorld(string name)
{
}

public int World(string name)
{
IAsyncResult aResult;
HelloWorldHandler handler = delegate(string name) {return decimal;};
aResult = handler.BeginInvoke(name, null, null, null);

if (!aResult.IsComplete)
aResult.AsyncWaitHandle.WaitOne();

decimal var1 =
((HelloWorldHandler)((AsyncResult)aResult).AsyncDe legate).EndInvoke(aResult);
}

There are lot of things I don't know here. Here no HelloWorldHandler foo =
HelloWorld; are used.. how can it still know what method to execute??

AFIAK the example above creates a delegate which execute in it's own thread.
started by BeginInvoke. and aResult.IsComplete check if the method reference
by the delegate is finished. And EndInvoke is called to retrieve the result
from the method.

Any suggestions?
Jan 3 '08 #3
On 3 Jan, 09:45, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Jeff <do...@spam.mewrote:
I'm trying to understand delegates. Below is an example I've created. I
don't understand how the delegate below becomes a reference pointer (AFIAK
delegates are reference pointers) to the method below it....
delegate decimal HelloWorldHandler(string name);
private decimal HelloWorld(string name)
{
}
any suggestions?

The first line defines a delegate *type*. You need a new *instance* of
the delegate, e.g.

HelloWorldHandler foo = HelloWorld;

--
Jon Skeet - <sk...@pobox.com>http://www.pobox.com/~skeet* Blog:http://www.msmvps.com/jon.skeet
World class .NET training in the UK:http://iterativetraining.co.uk
Incidently, Jon used Delegate Inference in the above.

HelloWorldHandler foo = HelloWorld;
would be:
HelloWorldHandler foo = new HelloWorldHandler(HelloWorld);
under 1.1, but it works in 2+ as well and you may well see both.
Jan 3 '08 #4
Jeff <do***@spam.mewrote:

<snip>
There are lot of things I don't know here. Here no HelloWorldHandler foo =
HelloWorld; are used.. how can it still know what method to execute??
It's using an anonymous method, here:

HelloWorldHandler handler = delegate(string name) {return decimal;};

Anonymous methods are a C# 2 features. See
http://pobox.com/~skeet/csharp/csharp2/delegates.html
AFIAK the example above creates a delegate which execute in it's own thread.
Well, it executes on a threadpool thread.

Have a look here for more on delegates:
http://pobox.com/~skeet/csharp/events.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jan 3 '08 #5
"Jeff" <do***@spam.mewrote in message
news:u%****************@TK2MSFTNGP04.phx.gbl...
thanks, I understand that now

However there are something here I don't understand. I'm trying to
understand some .NET code using delegates. Below is another example which
I
have trouble understanding. The example is taken from an app I'm
programming
on (names on methods are changed):
Something mystifies me in this code:-
delegate decimal HelloWorldHandler(string name);
private decimal HelloWorld(string name)
{
}

public int World(string name)
{
IAsyncResult aResult;
HelloWorldHandler handler = delegate(string name) {return decimal;};
aResult = handler.BeginInvoke(name, null, null, null);
Where does the BeginInvoke method come from? The base Delegate abstract
type doesn't have it.
>
if (!aResult.IsComplete)
aResult.AsyncWaitHandle.WaitOne();

decimal var1 =
((HelloWorldHandler)((AsyncResult)aResult).AsyncDe legate).EndInvoke(aResult)
;

Ditto for EndInvoke?
}

There are lot of things I don't know here. Here no HelloWorldHandler foo =
HelloWorld; are used.. how can it still know what method to execute??

AFIAK the example above creates a delegate which execute in it's own
thread.
started by BeginInvoke. and aResult.IsComplete check if the method
reference
by the delegate is finished. And EndInvoke is called to retrieve the
result
from the method.
Am missing something fundemental and just having an off day?

--
Anthony Jones - MVP ASP/ASP.NET
Jan 3 '08 #6
Anthony Jones <An*@yadayadayada.comwrote:

<snip>
IAsyncResult aResult;
HelloWorldHandler handler = delegate(string name) {return decimal;};
aResult = handler.BeginInvoke(name, null, null, null);

Where does the BeginInvoke method come from? The base Delegate abstract
type doesn't have it.
No - and it couldn't do, given that each delegate type would have its
own signature.

The compiler creates Invoke, BeginInvoke and EndInvoke methods for each
delegate type.

<snip>

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Jan 3 '08 #7

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

Similar topics

4
2155
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
15
5727
by: Sharon | last post by:
I’m trying to build a generic Publisher-Subscriber that will work over the net, so I’m using the Remoting. I wish that the subscriber user will be notify about the messages sent by the...
5
2090
by: Doug Handler | last post by:
Hi, I have a form (Form1) that contains a tab control which one tab has a customer user control (UserControl1). When the user double-clicks on the grid hosted there a new user control is...
1
1804
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
4
3375
by: Bill Woodruff | last post by:
< note : this message was sparked in part by comments by David Browne on a previous thread : "inserting an anonymous method as a value in a generic dictionary ?" : David had shown the use of...
11
2332
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
7
3129
by: Andreas Reiff | last post by:
Hey! I have a managed c++ app (actually, the app is mixed managed/unmanaged, but this happens in the managed part) that dynamically generates some program in c# (with CSharpCodeProvider). The...
3
1359
by: james | last post by:
Hi guys, I create a delegate and pass in a local variable. When the variable is a reference type everything works fine, but when it is a valuetype the delegate uses the value of the last...
26
3587
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
8
4361
by: Advait Mohan Raut | last post by:
Hello, I am using VC# 2005 ; C# 2.0 I am working on the performance measurement tool. For that I need my code to call user defined method along with its parameters. For that should I use a...
0
7037
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
6904
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
7076
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...
1
6732
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...
0
5324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4768
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...
0
2990
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...
0
1294
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 ...
1
558
muto222
php
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.