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

Delegates and the Invoke method

Dom
I'm teaching myself about delegates and the Invoke method, and I have
a few newbie questions for the gurus out there:

Here are some CSharp statements:
1. public delegate void MyDelegate (int k, string s)
2. MyDelegate MyDelegateVar = MyMethod
3. MyForm.Invoke (MyDelegateVar)

Some questions:

1. What is the meaning of "public" in the first statement? The help
examples always use that. Isn't "private" more appropriate?

2. What is the purpose of "k" and "s" in the parameter list?

3. Isn't this a needless complication over C's pointer to a function,
which did in one statement what is done in two statements above? It
doesn't seem to be any more type-safe than what we had in C.

3. MyForm.Left has a different meaning that MyButton.Left, but what
is the difference between MyForm.Invoke, and MyButton.Invoke? For
that matter, why do we not just have System.Invoke, or something like
that?
Thanks,
Dom

May 18 '07 #1
6 39409
Dom,

Public is a way of saying that anyone can access the delegate. If you
feel that no one else is going to be able to use the delegate, then by all
means, make it private (as long as you can access it properly from the class
you want to use it in).

k and s in the parameter list are a little superflouous. Technically,
you don't need a parameter name, but for the sake of consistency, it makes
sense. Also, things like intellisense would need to show something when you
try and invoke it. Remember that delegates can have more than one method
associated with them, so you can't just take the parameter names of the
underlying method.

It's absolutely more type-safe. In C, you could set a function pointer
to anything in memory, and when you tried to execute it, the program
crashed. In C#, if the signature does not match, then your program doesn't
compile.

The Form class and the Button class do not have a Left property. If you
declared them on each of those types separately, then they would be
different from Invoke on the Button and the Form class. The reason you see
them on both is because the Button class and the Form class have a common
base type which has the Invoke method declared on it.

Also, you should note that the Invoke method on the Form/Button class is
different from the Invoke method on a delegate. The Invoke method on a
Form/Button instance allows you to pass a delegate to it so that the
delegate is invoked on the thread that created the control.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dom" <do********@gmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
I'm teaching myself about delegates and the Invoke method, and I have
a few newbie questions for the gurus out there:

Here are some CSharp statements:
1. public delegate void MyDelegate (int k, string s)
2. MyDelegate MyDelegateVar = MyMethod
3. MyForm.Invoke (MyDelegateVar)

Some questions:

1. What is the meaning of "public" in the first statement? The help
examples always use that. Isn't "private" more appropriate?

2. What is the purpose of "k" and "s" in the parameter list?

3. Isn't this a needless complication over C's pointer to a function,
which did in one statement what is done in two statements above? It
doesn't seem to be any more type-safe than what we had in C.

3. MyForm.Left has a different meaning that MyButton.Left, but what
is the difference between MyForm.Invoke, and MyButton.Invoke? For
that matter, why do we not just have System.Invoke, or something like
that?
Thanks,
Dom

May 18 '07 #2
"Dom" <do********@gmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
Here are some CSharp statements:
1. public delegate void MyDelegate (int k, string s)
2. MyDelegate MyDelegateVar = MyMethod
3. MyForm.Invoke (MyDelegateVar)

1. What is the meaning of "public" in the first statement? The help
examples always use that. Isn't "private" more appropriate?
It means that "MyDelegate" will be visible from outside of the class
that declares it. If you are only going to use it inside the class then yes,
"private" would be more appropriate.
2. What is the purpose of "k" and "s" in the parameter list?
They are just placeholders used to represent the parameters for the
routines to which the delegate will point. The names themselves are not
significant. You could replace "parameter1" and "parameter2" instead of "k"
and "s", and the declaration of the delegate would be the same.
3. MyForm.Left has a different meaning that MyButton.Left, but what
is the difference between MyForm.Invoke, and MyButton.Invoke?
If MyButton belongs to MyForm they do the same thing: They cause the
delegate to be invoked in the thread that is processing the windows form UI.
Since Form inherits from Control, you are just calling the Invoke method in
the Control class in both cases.

May 18 '07 #3
Dom
Thanks for the info, gives me a better mental picture, but I'm
confused about this.
It's absolutely more type-safe. In C, you could set a function pointer
to anything in memory, and when you tried to execute it, the program
crashed. In C#, if the signature does not match, then your program doesn't
compile.
If I remember by C days, the pointer was declared like this:

int (*ptrFunction) (int, string)

.... and I think the compiler did fail if you set ptrFunction to a
function with the wrong signature. Am I wrong about that?

Dom
May 18 '07 #4
Dom <do********@gmail.comwrote:
I'm teaching myself about delegates and the Invoke method, and I have
a few newbie questions for the gurus out there:

Here are some CSharp statements:
1. public delegate void MyDelegate (int k, string s)
2. MyDelegate MyDelegateVar = MyMethod
3. MyForm.Invoke (MyDelegateVar)

Some questions:

1. What is the meaning of "public" in the first statement? The help
examples always use that. Isn't "private" more appropriate?
You're declaring the delegate *type* in line 1. Whether it should be
public or not depends on the use, but it's not as if you're making a
field public.
2. What is the purpose of "k" and "s" in the parameter list?
That's up to the documentation for MyDelegate to decide. The names
themselves don't matter, if that's what you're asking about, but it's a
lot easier to write documentation in terms of named parameters than
"the first parameter is..." etc.
3. Isn't this a needless complication over C's pointer to a function,
which did in one statement what is done in two statements above? It
doesn't seem to be any more type-safe than what we had in C.
I find C's pointer-to-function syntax incredibly hard to read, for one
thing. Here, once you've seen the delegate declaration, that's the only
place you need that. Also, as Nicholas points out, it's type safe.
3. MyForm.Left has a different meaning that MyButton.Left, but what
is the difference between MyForm.Invoke, and MyButton.Invoke? For
that matter, why do we not just have System.Invoke, or something like
that?
Control.Invoke effectively marshals a delegate call to the owning
thread for that control. Beyond choosing which thread should be used,
which control you pick makes no difference. However, there may be more
than one UI thread, or none at all, which is why a static method
somewhere wouldn't make sense.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 18 '07 #5
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:

<snip>
k and s in the parameter list are a little superflouous. Technically,
you don't need a parameter name, but for the sake of consistency, it makes
sense.
Well, you don't need it in an abstract sense, but the C# compiler
certainly demands it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 18 '07 #6
Absolutely in the abstract sense. Kids demand things all the time that
they don't need.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:

<snip>
> k and s in the parameter list are a little superflouous.
Technically,
you don't need a parameter name, but for the sake of consistency, it
makes
sense.

Well, you don't need it in an abstract sense, but the C# compiler
certainly demands it.

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

May 18 '07 #7

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

Similar topics

4
by: Bob Rock | last post by:
Hello, I was wondering if callback delegates, called in response to a event, are executed in their own thread. I was suspecting the OS might spawn a new thread and have the delegate execute in...
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...
12
by: Grant | last post by:
I am having great difficulty understanding this and any code samples I find online are kilometres long and complicated to understand...Please could someone give me a simple exampe of how to get a...
15
by: Iced Crow | last post by:
In C# I know that you can use delegates to assing multiple addresses of sub and functions to a delegate and have it fire multiple procedures... How do I do this in VB? I only know of assigning...
4
by: Tim | last post by:
There are a set of clients who need to be notified of certain events. I have used events and delegates (publisher-Subscriber model) for the notification mechanism. All the clients register with...
3
by: Pavils Jurjans | last post by:
(I am sorry to crosspost both here and in asp.net group. I think, this question quite certainly belongs better to this group, but by error I posted the question to asp.net group) Hello, I am...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
15
by: =?Utf-8?B?VG9tIENvcmNvcmFu?= | last post by:
I've been led to believe by several articles, particularly Eric Gunnerson's C# Calling Code Dynamically, that calling a method dynamically through Reflection was much slower than through a...
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
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?
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
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...
0
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
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...
0
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,...
0
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...

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.