PC Review


Reply
Thread Tools Rate Thread

Check if handler has been assigned to the delegate

 
 
=?Utf-8?B?TWF0cyBMYXJzc29u?=
Guest
Posts: n/a
 
      16th Mar 2005
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)

VB Code:
Public Event CellConsumerInit As CellConsumerInitEventHandler
If 'and how do I write here' then

Mats

 
Reply With Quote
 
 
 
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      16th Mar 2005
"Mats Larsson" <(E-Mail Removed)> schrieb:
> 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)
>
> VB Code:
> Public Event CellConsumerInit As CellConsumerInitEventHandler
> If 'and how do I write here' then


\\\
Public Module Program
Public Sub Main()
Dim c As New FooBar()
AddHandler c.Foo, AddressOf Goo
c.AddSampleHandler()
c.AddSampleHandler()
Console.WriteLine( _
"Anzahl der Handler für Foo: {0}", _
c.NumberOfFooHandlers _
)
RemoveHandler c.Foo, AddressOf Goo
Console.Read()
End Sub

Private Sub Goo()
End Sub
End Module

Public Class FooBar
Public Event Foo()

Public ReadOnly Property NumberOfFooHandlers() As Integer
Get
If FooEvent Is Nothing Then
Return 0
Else
Return FooEvent.GetInvocationList().Length
End If
End Get
End Property

Public Sub AddSampleHandler()
AddHandler Foo, AddressOf Moo
End Sub

Private Sub Moo()
End Sub
End Class
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

 
Reply With Quote
 
=?Utf-8?B?TWF0cyBMYXJzc29u?=
Guest
Posts: n/a
 
      16th Mar 2005
I'm sorry to say that the code from Herfried didn't answer my question or I
didn't understand the answer.

I don't understand the line 'If FooEvent Is Nothing Then' because I can't
finde the definition for FooEvent, and in my case I don't have any object
referens because the event is to an other webpart, or do I ? there i can
apply the test.

I'm trying to write a webpart for sharepoint and I fetched the Northwind
example for a cell consumer webpar but it was written in C# and I write all
my code in VB so I converted the code to VB, this is the only part I haven't
manage to convert.

The event is sent to an other webpart. In the example there is this test to
se if there is any other webpart connected to this befor generating data and
sending the event.

Once again the code, this time some more code.
In C#:
public event CellConsumerInitEventHandler CellConsumerInit;
' ...and if a handler has been assigned to the delegate.
if (CellConsumerInit != null)
{
CellConsumerInit(this, cellConsumerInitEventArgs);
}
in VB:
Public Event CellConsumerInit As CellConsumerInitEventHandler
' ...and if a handler has been assigned to the delegate.
If CellConsumerInit = Nothing Then 'This dosen't work
RaiseEvent CellConsumerInit(Me, cellConsumerInitEventArgs)
End If



"Herfried K. Wagner [MVP]" wrote:

> "Mats Larsson" <(E-Mail Removed)> schrieb:
> > 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)
> >
> > VB Code:
> > Public Event CellConsumerInit As CellConsumerInitEventHandler
> > If 'and how do I write here' then

>
> \\\
> Public Module Program
> Public Sub Main()
> Dim c As New FooBar()
> AddHandler c.Foo, AddressOf Goo
> c.AddSampleHandler()
> c.AddSampleHandler()
> Console.WriteLine( _
> "Anzahl der Handler für Foo: {0}", _
> c.NumberOfFooHandlers _
> )
> RemoveHandler c.Foo, AddressOf Goo
> Console.Read()
> End Sub
>
> Private Sub Goo()
> End Sub
> End Module
>
> Public Class FooBar
> Public Event Foo()
>
> Public ReadOnly Property NumberOfFooHandlers() As Integer
> Get
> If FooEvent Is Nothing Then
> Return 0
> Else
> Return FooEvent.GetInvocationList().Length
> End If
> End Get
> End Property
>
> Public Sub AddSampleHandler()
> AddHandler Foo, AddressOf Moo
> End Sub
>
> Private Sub Moo()
> End Sub
> End Class
> ///
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://classicvb.org/petition/>
>
>

 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      16th Mar 2005
Mats,
In VB.NET you can simply use RaiseEvent, it automatically checks for no
handlers!

> in VB:
> Public Event CellConsumerInit As CellConsumerInitEventHandler


Protected Overridable Sub OnCellConsumerInit(e As EventArgs)
RaiseEvent CellConsumerInit(Me, e)
End Sub


> I don't understand the line 'If FooEvent Is Nothing Then' because I can't
> finde the definition for FooEvent, and in my case I don't have any object
> referens because the event is to an other webpart, or do I ? there i can
> apply the test.


When you define "Event Foo" VB.NET automatically adds a field call
"FooEvent". The "FooEvent" will not show up in the IDE or Intellisense,
however you are free to use it in your code.

Hope this helps
Jay


"Mats Larsson" <(E-Mail Removed)> wrote in message
news:16C79ED5-5355-4742-93F0-(E-Mail Removed)...
> I'm sorry to say that the code from Herfried didn't answer my question or
> I
> didn't understand the answer.
>
> I don't understand the line 'If FooEvent Is Nothing Then' because I can't
> finde the definition for FooEvent, and in my case I don't have any object
> referens because the event is to an other webpart, or do I ? there i can
> apply the test.
>
> I'm trying to write a webpart for sharepoint and I fetched the Northwind
> example for a cell consumer webpar but it was written in C# and I write
> all
> my code in VB so I converted the code to VB, this is the only part I
> haven't
> manage to convert.
>
> The event is sent to an other webpart. In the example there is this test
> to
> se if there is any other webpart connected to this befor generating data
> and
> sending the event.
>
> Once again the code, this time some more code.
> In C#:
> public event CellConsumerInitEventHandler CellConsumerInit;
> ' ...and if a handler has been assigned to the delegate.
> if (CellConsumerInit != null)
> {
> CellConsumerInit(this, cellConsumerInitEventArgs);
> }
> in VB:
> Public Event CellConsumerInit As CellConsumerInitEventHandler
> ' ...and if a handler has been assigned to the delegate.
> If CellConsumerInit = Nothing Then 'This dosen't work
> RaiseEvent CellConsumerInit(Me, cellConsumerInitEventArgs)
> End If
>
>
>
> "Herfried K. Wagner [MVP]" wrote:
>
>> "Mats Larsson" <(E-Mail Removed)> schrieb:
>> > 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)
>> >
>> > VB Code:
>> > Public Event CellConsumerInit As CellConsumerInitEventHandler
>> > If 'and how do I write here' then

>>
>> \\\
>> Public Module Program
>> Public Sub Main()
>> Dim c As New FooBar()
>> AddHandler c.Foo, AddressOf Goo
>> c.AddSampleHandler()
>> c.AddSampleHandler()
>> Console.WriteLine( _
>> "Anzahl der Handler für Foo: {0}", _
>> c.NumberOfFooHandlers _
>> )
>> RemoveHandler c.Foo, AddressOf Goo
>> Console.Read()
>> End Sub
>>
>> Private Sub Goo()
>> End Sub
>> End Module
>>
>> Public Class FooBar
>> Public Event Foo()
>>
>> Public ReadOnly Property NumberOfFooHandlers() As Integer
>> Get
>> If FooEvent Is Nothing Then
>> Return 0
>> Else
>> Return FooEvent.GetInvocationList().Length
>> End If
>> End Get
>> End Property
>>
>> Public Sub AddSampleHandler()
>> AddHandler Foo, AddressOf Moo
>> End Sub
>>
>> Private Sub Moo()
>> End Sub
>> End Class
>> ///
>>
>> --
>> M S Herfried K. Wagner
>> M V P <URL:http://dotnet.mvps.org/>
>> V B <URL:http://classicvb.org/petition/>
>>
>>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      16th Mar 2005
Jay,

"Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> schrieb:
> In VB.NET you can simply use RaiseEvent, it automatically checks for no
> handlers!


ACK, but this doesn't help to decide whether or not certain operations
should be executed iff at least one handler has been added to the event.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      16th Mar 2005
Herfried,
Which is why I explained where the "FooEvent" field was coming from. :-)


Realistically how often does anyone conditionally do something based on
handlers on *events*?


I will do things conditionally based on handlers on Delegates, however in
this case the delegates are callbacks & not Events. Generally this is simple
to see if I need to invoke the callback or not...

Just a thought
Jay


"Herfried K. Wagner [MVP]" <hirf-spam-me-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Jay,
>
> "Jay B. Harlow [MVP - Outlook]" <(E-Mail Removed)> schrieb:
>> In VB.NET you can simply use RaiseEvent, it automatically checks for no
>> handlers!

>
> ACK, but this doesn't help to decide whether or not certain operations
> should be executed iff at least one handler has been added to the event.
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://classicvb.org/petition/>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How To Test For Assigned Event Handler Ratfish Microsoft C# .NET 1 4th Dec 2009 06:06 AM
Can we add an event handler that has different delegate? Tee Microsoft C# .NET 2 9th Feb 2005 06:43 PM
delegate or event handler? ALI-R Microsoft C# .NET 6 6th Nov 2004 06:28 AM
Expert: How to check if an event handler has a delegate? Henri Microsoft ASP .NET 0 31st Oct 2004 08:00 PM
How to know if a delegate has been assigned to an event???? Bob Rock Microsoft C# .NET 4 30th Jul 2004 11:43 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:29 AM.