Problem with converting "eventhandlers" from c# to vb.net

csharp , vb.net Jordan
  • 10 years ago

    Hi all
    i have trouble with converting some pices of my old c# code to vb.net and hope someone here can help me.

    Code :

    Public NotInheritable Class ......

     Public Shared Event ResolvingServiceProvider As EventHandler(Of ResolvingServiceProviderEventArgs)
     ....
     ....
     
     Public Shared Function CreateProvider(ByVal providerType As Type) As IServiceProvider
     
      Dim provider As IServiceProvider = CreateFromType(providerType)
      
      ' working c# code
      'if (ResolvingServiceProvider != null)
      '{
      'ResolvingServiceProviderEventArgs e = new ResolvingServiceProviderEventArgs(provider, null);
      'ResolvingServiceProvider(null, e);
      'provider = e.ServiceProvider;
      '}
      

      ' New VB.Net code (2005)
      If ResolvingServiceProvider IsNot Nothing Then   ' Error (*)

       Dim e As New ResolvingServiceProviderEventArgs(provider, Nothing)
       RaiseEvent ResolvingServiceProvider(Nothing, e) ' Working
       provider = e.ServiceProvider
      End If

      ... 

                Return provider
        
            End Function

     ...
     
    End Class 

     (*)  ' Error (Fehler 4 "Public Shared Event ResolvingServiceProvider(sender As Object, e As ResolvingServiceProviderEventArgs)" ist ein Ereignis und kann nicht direkt aufgerufen werden. Verwenden Sie eine RaiseEvent-Anweisung, um ein Ereignis aufzurufen.)

     I tryed to use the RaiseEvent also for the if statement but this dont work too

    PS : Excuses my bad English. My school time is past already 30 years ;-)

     

  • 10 years ago

    Hi there,

    I'm sorry, I can't  understand your question. Do you want to create an custom event in VB.NET? if it's true, see the following code:

        Public ResolvingServiceProviderHandler As EventHandler

    Public Custom Event ResolvingServiceProvider As EventHandler
    AddHandler(ByVal value As EventHandler)
    ResolvingServiceProviderHandler = value
    End AddHandler
    RemoveHandler(ByVal value As EventHandler)
    ResolvingServiceProviderHandler = Nothing
    End RemoveHandler
    RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)
    ResolvingServiceProviderHandler.Invoke(sender, e)
    End RaiseEvent
    End Event

    Public Overridable Sub OnResolvingServiceProvider(ByVal e As EventArgs)
    If (ResolvingServiceProviderHandler IsNot Nothing) Then
    RaiseEvent ResolvingServiceProvider(Me, e)
    End If
    End Sub
  • 10 years ago
    You just use the RaiseEvent statement.  You don't test the event itself to see whether it has any handlers registered as that's not supports in VB.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” - Rick Osborne