Check if handler has been assigned to the delegate

G

Guest

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
 
H

Herfried K. Wagner [MVP]

Mats Larsson said:
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
///
 
G

Guest

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
 
J

Jay B. Harlow [MVP - Outlook]

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
 
H

Herfried K. Wagner [MVP]

Jay,

Jay B. Harlow said:
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.
 
J

Jay B. Harlow [MVP - Outlook]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top