471,624 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,624 software developers and data experts.

Check if handler has been assigned to the delegate

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

Nov 21 '05 #1
5 8007
"Mats Larsson" <Ma*********@discussions.microsoft.com> 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/>

Nov 21 '05 #2
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" <Ma*********@discussions.microsoft.com> 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/>

Nov 21 '05 #3
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" <Ma*********@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com... 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" <Ma*********@discussions.microsoft.com> 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 fr 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/>

Nov 21 '05 #4
Jay,

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> 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/>

Nov 21 '05 #5
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]" <hi***************@gmx.at> wrote in message
news:e1**************@TK2MSFTNGP09.phx.gbl...
Jay,

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> 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/>

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Pavils Jurjans | last post: by
3 posts views Thread by Beth | last post: by
5 posts views Thread by Charles Bazi | last post: by
6 posts views Thread by mj2736 | last post: by
24 posts views Thread by =?Utf-8?B?U3dhcHB5?= | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

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.