473,408 Members | 1,747 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: How to check for target methods

Hi,

I'm using delegates to show progress when performing certain tasks. However,
I'd like to check to see that there actually is a method available for the
delegate to call. What's the correct way of doing that? Looking at the
documentation for delegates, I've come up with:

if (MyEvent.Target != null)
{
MyEvent(this, new MyEventArgs());
}

Still, this would be correct for instance methods only, right? Would this be
better?

if (MyEvent.Method != null)
{
MyEvent(this, new MyEventArgs());
}

Thanks,
Einar.

--
"If it was so, it might be; and if it were so, it would be; but as it isn't,
it ain't. That's logic" -- Lewis Carroll
Nov 16 '05 #1
4 1356
Einar Høst <_e*******@hotmail.com> wrote:
I'm using delegates to show progress when performing certain tasks. However,
I'd like to check to see that there actually is a method available for the
delegate to call. What's the correct way of doing that? Looking at the
documentation for delegates, I've come up with:

if (MyEvent.Target != null)
{
MyEvent(this, new MyEventArgs());
}

Still, this would be correct for instance methods only, right? Would thisbe
better?

if (MyEvent.Method != null)
{
MyEvent(this, new MyEventArgs());
}


Could you give an example of how you'd construct a delegate which
*didn't* have a method to call?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Einar,
However,
I'd like to check to see that there actually is a method available for the
delegate to call.


As long as the delegate isn't a null reference there is at least one
method to call, so you just have to do

MyEventHandler h = MyEvent;
if ( h != null )
h(this, new MyEventArgs());

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #3
"Einar Høst" <_e*******@hotmail.com> wrote in message news:<eq**************@TK2MSFTNGP09.phx.gbl>...
Hi,

I'm using delegates to show progress when performing certain tasks. However,
I'd like to check to see that there actually is a method available for the
delegate to call. What's the correct way of doing that? Looking at the
documentation for delegates, I've come up with:

if (MyEvent.Target != null)
{
MyEvent(this, new MyEventArgs());
}

Still, this would be correct for instance methods only, right? Would this be
better?

if (MyEvent.Method != null)
{
MyEvent(this, new MyEventArgs());
}

Thanks,
Einar.


You should simply check if the delegate variable is null. An example:

[...]
private delegate void MyEventDelegate();

private MyEventDelegate myEvent;

private void button2_Click(object sender, System.EventArgs e) {
if (myEvent != null) {
myEvent();
} else {
Console.WriteLine("Empty Delegate. Adding methods");
myEvent += new MyEventDelegate(MyStaticMethod);
myEvent += new MyEventDelegate(MyInstanceMethod);
}
}

private static void MyStaticMethod() {
Console.WriteLine("Hello from MyStaticMethod()");
}

private void MyInstanceMethod() {
Console.WriteLine("Hello from MyInstanceMethod()");
}
[...]
Nov 16 '05 #4
Hi, thanks for responses, I believe 'if (MyEvent != null)' does the trick.
Obviously, I'm new to the whole delegate thing, so my understanding of it is
a bit muddled.

- Einar
Nov 16 '05 #5

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

Similar topics

10
by: Brad | last post by:
Question for all: Would one consider this a loophole in .net with respect to nested (inner) classes? 1) Create outer class Foo and create a nested class Yin (inside Foo's class definition...
2
by: Marcos Stefanakopolus | last post by:
In C# is there any way to use the concept of delegates to have multiple implementations of a particular block of code, so that you can choose between them without the overhead of possibly expensive...
5
by: Rene | last post by:
I created a class (SomeClass), on that class, I declared a delegate (OnXyz) and then declared an event based on that delegate (Xyz). All this in the same class. After that, I created another class...
1
by: Christoph Nahr | last post by:
The task: I want to let a background thread and a Windows Forms foreground thread communicate via callback methods. Now synchronization of a *single* delegate works just fine with the usual...
2
by: Matthew Herrmann | last post by:
Hi, I've heard from groups that listeners to event handlers cause references to be kept alive, if the targets are marked to stay alive. I need to make sure that attaching events to objects will...
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...
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...
0
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
4
by: Frankie | last post by:
The following questions are with respect to this delegate declaration: delegate void MyDelegate(string s); Question 1: Why does the following NOT work? Why can't I just create a new instance...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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.