473,385 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 8209
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bob Rock | last post by:
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???...
1
by: Ivan | last post by:
subject says it all. thanks in advance. Ivan
3
by: Pavils Jurjans | last post by:
Hello, I am looking for solution to assign the Session.onEnd event handler dynamically, at runtime, without using global.asax file. I am a bit sceptic wether that is possible, however I thought...
3
by: Doug | last post by:
Hi all, Does anyone a way to determine if a handler has been assigned to a delegate, at runtime? I'm converting some C# code and it tests whether or not the handler has been assigned. ...
3
by: Beth | last post by:
in the following: this.ExitButton.Click += new System.EventHandler(this.ExitButton_Click); if I saw an equation, such as y +=x; then y = y+x. But what is the meaning in the event handler. I...
5
by: Charles Bazi | last post by:
Hi, I have a base UserControl named UserControlEx. I have created an event, and a dummy handler. Then I create several UCs with UserControlEx base. Some of those have a handler for my custom...
2
by: Ralph | last post by:
Hi I don't understand why it's not working: function schedule(imTop){ this.tdImagesTop = imTop; } schedule.prototype.selectEl = function() { alert(this.tdImagesTop);
6
by: mj2736 | last post by:
I created a windows form control that inherits from the standard .Net DataGridView control, to which I've added custom functionality. Instead of using the standard control in my applications, I use...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.