I missed it earlier, but Mike Woodring actually gave the answer to this
in an earlier post.
To follow up on this, obviously for things other than events, C#'s
"something == null" is the same as VB's "something Is Nothing". For
event handlers, though, C#'s "someevent == null" is the same as VB's
"someeventevent Is Nothing".
VB autogenerates a private member named <EventName>Event for the
delegate. It *is* accessible (as long as you have access to private
members, of course), but is hidden from Intellisense. So, this seems to
be the most accurate conversion from C#:
<code lang="C#">
class EventSender {
protected virtual void OnTest(EventArgs e) {
if (this.Test != null) {
this.Test.Invoke(this, e);
}
}
public EventHandler Test;
}
</code>
<code lang="VB">
Public Class EventSender
Protected Overridable Sub OnTest(e as EventArgs)
If Me.TestEvent IsNot Nothing Then
Me.TestEvent.Invoke(Me, e)
End If
End Sub
Public Event Test as EventHandler
End Class
</code>
Which means the answer to the original question is...check if
<EventName>Event is Nothing to see if anyone is subscribed. Of course,
if you don't have control of the class, then you'll have to use
reflection as it's a private member.
--MB
-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Mark Brackett
Sent: Wednesday, February 14, 2007 3:56 PM
To: [email protected]
Subject: Re: [ADVANCED-DOTNET] Determining if an event has been assigned
a delegate...
For VB.NET (which the OP seemed to be in), I haven't found an easy
analog to C#'s "something == null" syntax.
In VB.NET 2005, you can do a Custom Event and have your own AddHandler
routine, which gets stored in a delegate that you can then check...but
otherwise, I don't think it can be done without Reflection.
--MB
-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of gregory young
Sent: Wednesday, February 14, 2007 3:31 PM
To: [email protected]
Subject: Re: [ADVANCED-DOTNET] Determining if an event has been assigned
a delegate...
The event is seen differently from outside the class .. try this. I
think you can only do this for classes that you have control over.
public class Test
{
public event EventHandler Something;
public void DoSomething()
{
if (Something != null)
{
Something(this, null);
}
}
public void PrintListenners()
{
PrintListenners(Something);
}
static void PrintListenners(Delegate del)
{
foreach (Delegate d in del.GetInvocationList())
{
Console.WriteLine("Method Name: {0}", d.Method);
Console.WriteLine("Type Name: {0}", d.Target);
}
}
}
class Program
{
static void Main(string[] args)
{
Test t = new Test();
t.Something += new EventHandler(t_Something);
t.Something += new EventHandler(t_Something2);
t.PrintListenners();
}
static void t_Something2(object sender, EventArgs e)
{
Console.Write("Something2");
}
static void t_Something(object sender, EventArgs e)
{
Console.Write("Something");
}
}
Cheers,
Greg
On 2/14/07, Mike Andrews <[EMAIL PROTECTED]> wrote:
> Thank you
>
> After some investigation, I cannot seem to get this to work.
>
> I created this method:
>
> private static void DisplayDelegate(Delegate obj) {
> foreach (Delegate d in obj.GetInvocationList()) {
> Console.WriteLine("Method Name: {0}", d.Method);
> Console.WriteLine("Type Name: {0}", d.Target);
> }
> }
>
> and called the method as such:
>
> DisplayDelegate(cb.CheckedChanged)
>
> where
> cb is a System.Web.UI.WebControls.CheckBox
>
> and the compiler tells me:
> The event 'System.Web.UI.WebControls.CheckBox.CheckedChanged' can only
> appear on the left hand side of += or -=
>
> What might I be doing incorrectly here?
> Is what I want to do even possible?
>
> Thanks,
> Mike
>
>
>
>
> On 2/14/07, Phil Sayers <[EMAIL PROTECTED]> wrote:
> >
> > google for GetInvocationList
> > that should point you in the right direction
> >
> >
> > -----Original Message-----
> > From: Discussion of advanced .NET topics.
> > [mailto:[EMAIL PROTECTED] Behalf Of Mike
Andrews
> > Sent: Wednesday, February 14, 2007 2:52 PM
> > To: [email protected]
> > Subject: [ADVANCED-DOTNET] Determining if an event has been assigned
a
> > delegate...
> >
> >
> > Guys,
> >
> > Do any of you know if it's possible to determine if an event for an
object
> > has been assigned a delegate?
> >
> > Example:
> >
> > Dim cb As New CheckBox()
> > cb.Name = "MyCheckBox"
> > AddHandler(cb.CheckChanged, AddressOf CheckChanged)
> >
> > ...
> >
> > Public Sub CheckChanged(Object sender, EventArgs e)
> > ...
> > End Sub
> >
> > Is it possible to know if cb.CheckChanged has been assigned a
handler or
> > not?
> >
> > Thanks,
> > Mike
> >
> > ===================================
> > This list is hosted by DevelopMentor(r) http://www.develop.com
> >
> > View archives and manage your subscription(s) at
> > http://discuss.develop.com
> >
> > ===================================
> > This list is hosted by DevelopMentor(r) http://www.develop.com
> >
> > View archives and manage your subscription(s) at
> > http://discuss.develop.com
> >
>
> ===================================
> This list is hosted by DevelopMentor(r) http://www.develop.com
>
> View archives and manage your subscription(s) at
http://discuss.develop.com
>
--
Studying for the Turing test
===================================
This list is hosted by DevelopMentor(r) http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
===================================
This list is hosted by DevelopMentor(r) http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com