473,795 Members | 3,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to know if a delegate has been assigned to an event????

Hello,

is there a way to dynamically query if a callback delegate has been
tied to an event to avoid raising the event if none have been tied or
raise an exception if proper for the situation???
Thx.
Bob Rock
Nov 16 '05 #1
4 2198
Bob Rock <an************ ***@yahoo.com> wrote:
is there a way to dynamically query if a callback delegate has been
tied to an event to avoid raising the event if none have been tied or
raise an exception if proper for the situation???


The normal way is:

if (myEventDelegat e==null)
{
....
}

That's assuming the code which needs to find this out is within the
class declaring the event - you can't find out from outside.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Try this:
if (myEventDelegat e != null)
{
myEventDelegate (...);
}

Delegate types represent references to methods with a particular signature and return type. They make it possible to treat methods as objects that can be assigned to variables and passed as parameters. As such they can be compared to null just as easily as any other reference.

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.
"Bob Rock" wrote:
Hello,

is there a way to dynamically query if a callback delegate has been
tied to an event to avoid raising the event if none have been tied or
raise an exception if proper for the situation???
Thx.
Bob Rock

Nov 16 '05 #3
You can get the delegates invocation list using GetInvocationLi st. The count
of objects in this array is the number of handlers tied to the event. The
order in which they are presented in this list is the order in which the
event will call them.

--
Bob Powell [MVP]
Visual C#, System.Drawing

The Image Transition Library wraps up and LED style instrumentation is
available in the June of Well Formed for C# or VB programmers
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml


"Bob Rock" <an************ ***@yahoo.com> wrote in message
news:98******** *************** ***@posting.goo gle.com...
Hello,

is there a way to dynamically query if a callback delegate has been
tied to an event to avoid raising the event if none have been tied or
raise an exception if proper for the situation???
Thx.
Bob Rock

Nov 16 '05 #4
"Bob Powell [MVP]" <bob@_spamkille r_bobpowell.net > wrote in message news:<O0******* ******@TK2MSFTN GP09.phx.gbl>.. .
You can get the delegates invocation list using GetInvocationLi st. The count
of objects in this array is the number of handlers tied to the event. The
order in which they are presented in this list is the order in which the
event will call them.

--
Bob Powell [MVP]
Visual C#, System.Drawing


Bob, thank you. Unfortunately doing myEvent.GetInvo cationList().Le ngth
causes an exception if the event has no delegated tied to it ... there
is no array in that case.

Jon controlling the null value on the event works perfectly well.
Bob Rock
Nov 16 '05 #5

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

Similar topics

1
1531
by: Serge Shimanovsky | last post by:
Hi, I have a situation where I need to clear the event sinks from an event. But let's say I don't know which methods (by name, I mean) signed up for that event. I do have a control with lets say two events: ItemCheck and DataSourceChange. Callbacks assigned to ItemCheck and DataSourceChange
4
2524
by: Jon Davis | last post by:
If two delegates are created that point to the exact same method, and an event is assigned both delegates, ... myObj.MyEvent += new EventHandler(MyHandler); myObj.MyEvent += new EventHandler(MyHandler); .... does it recognize the duplicate method and ignore the second one, or does it execute the same method twice when raised? Jon
3
1983
by: Max Khitrov | last post by:
Hello everyone, I ran into a small problem here and just can't figure it out. I'm writing a VS .NET add-in which creates a new commandbar and populates it with a few buttons. Each button is assigned a delegate for the Click event, but what seems to happen is that it works the first 2 times, and then the button does nothing after that. The delegate doesn't appear to be called anymore. Below are a few parts of the code related to this...
3
2618
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
5
8251
by: Mats Larsson | last post by:
Hi, I have an example in C# for testing if a handler has been assigned to the delegate, I would like to do the same in VB.NET but I don't understand how. The C# code is copied from the Northwind webpart example. C# Code: public event CellConsumerInitEventHandler CellConsumerInit; if (CellConsumerInit != null)
5
4240
by: han zhiyang | last post by:
Hi. I tried to design a custom web control which can flexibly and dynamicly let the control user ,for example the web page developer, customize its layout codes.This control derives from System.Web.UI.Control,and for my purpose,I define a delegate property in the control's class definition, so control user can define his(or her) method out of the class definition and add his(or her) method to the delegate chain. I also override the Render...
4
2022
by: ^MisterJingo^ | last post by:
Hi all, I've been trying to get my head around delegates. The book i'm using had a single example, not much explaination, and didn't show how to set up a delegate and pass variables in and out of the functions it refers to. So I've been playing around and came up with he following code. I know it doesn't do much, but I just wanted to fit together delegates and parameter passing: //////////////// Code start
10
10001
by: Nathan Laff | last post by:
I have a custom attribute which i use for fields in an enum. I want to pass around a delegate in these things. so i want to do something like this Is this possible? i'm having no luck.
6
1405
by: damiensawyer | last post by:
Hi, Can someone please explain to me something about delegates? My understanding is as follows. A delegate is basically an object that can hold a reference to a "method" somewhere. That is, it's essentially a pointer to a piece of code somewhere else in memory. It therefore (to me anyway) makes sense to define delegates with their signatures and be able to use those signatures at different points in
0
9519
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
10213
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
10163
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
9040
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
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
6780
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();...
1
4113
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
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.