Events

  • Thread starter Thread starter Frank D
  • Start date Start date
F

Frank D

I have a slight problem with writing the if statement of
the event

Here is a C# example

protected virtual void OnClick(EventArgs e)
{
if (Click != null)
{
//Invokes the delegates.
Click(this, e);
}
}
}

what I want to do is basically the same and this is what I
got currently:

Protected Overridable Sub OnClick(ByVal e As EventArgs)
'If Not (OnClick Is Nothing) Then
RaiseEvent Click(Me, e)
'End If
End Sub

TIA
 
Hello,

Frank D said:
I have a slight problem with writing the if statement of
the event

Here is a C# example

protected virtual void OnClick(EventArgs e)
{
if (Click != null)
{
//Invokes the delegates.
Click(this, e);
}
}
}

The sample below will show you how to get the number of handles registered
for a specific event:

\\\
Public Class Main
Public Shared 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 Shared Sub Goo()
End Sub
End Class

Public Class FooBar
Public Event Foo()

Public ReadOnly Property NumberOfFooHandlers() As Integer
Get
Return FooEvent.GetInvocationList().Length
End Get
End Property

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

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

HTH,
Herfried K. Wagner
 
Herr Cor,

Cor said:
I think C# is a lot less complex than VB.Net.
So many VB.net lines for just 9 foo C# lines?

I am a little 8/1 :-) nobody saw my translation from fu in bu in the class i
exstended.

In VB .NET there is no reason for testing if a handler is registered for the
event. You can simply use RaiseEvent to raise the event.

Regards,
Herfried K. Wagner
 
Back
Top