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/>
>>
>>
|