How to Convert This C# Event/Delegate Code Into VB

2019-09-19 13:12发布

问题:

C# to VB.NET Here is the relevant C# Code

namespace MyApp {
   public delegate bool AllocHandlerDelegate(int param1);

   public interface ILoader  {
    event AllocHandlerDelegate evAlloc;
    bool Load();
    }

   public class MyLoader : ILoader  {
    public event AllocHandlerDelegate evAlloc;

        public bool Load() {
            try  {
           if (this.evAlloc != null && !evAlloc(1))
                          return false;
        }
   }
}

Here is what I came up with so far. The C# delegate is a function which returns a boolean result. So I converted the delegate definition and event declaration as follow.

    Public Delegate Function AllocHandlerDelegate(ByVal param1 As Integer) As Boolean
    Public Event evAlloc As AllocHandlerDelegate

Visual Studio Returned with the Error Message: "Events cannot be declared with a delegate type that has a return type."

So I made it a SUB instead and completed the translation like this*.

Namespace MyApp
    Public Delegate Sub AllocHandlerDelegate(ByVal param1 As Integer)

    Public Interface ILoader
        Event evAlloc As AllocHandlerDelegate

    Function Load() As Boolean
    End Interface

    Public Class MyLoader
        Implements ILoader

        Public Event evAlloc As AllocHandlerDelegate Implements ILoader.evAlloc

        Public Function Load() as Boolean Implements ILoad.Load
            Try
                    If Me.evAllocEvent IsNot Nothing AndAlso Not evAlloc(1) Then
                        Return False
            End Try
        End Function
    End Class
End Namespace

*Note: I already realized that I could not check for the "Event" object to be nothing, but for the VB internal Delegate Object *EVENTNAME*Event.

However for the second part I don't know what to do with it. There I get the following Error message: 'Public Event evAlloc (param1 As Integer)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

Any Ideas?! Thank You

回答1:

The error message is pretty clear:

Public Event evAlloc (param1 As Integer)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

It's an event, you can't not call it directly. Use a 'RaiseEvent' statement instead:

RaiseEvent evAlloc(1)

Your event can't return a value (the C# equivalent can), but you can use a plain delegate instead.

Public Delegate Function AllocHandlerDelegate(ByVal param1 As Integer) As Boolean

Public Interface ILoader
    Property evAlloc As AllocHandlerDelegate
    Function Load() As Boolean
End Interface

Public Class MyLoader
    Implements ILoader

    Public Property evAlloc As AllocHandlerDelegate Implements ILoader.evAlloc

    Public Function Load() as Boolean Implements ILoader.Load
            If Me.evAlloc IsNot Nothing AndAlso Not evAlloc(1) Then _
                Return False
            ' Do Stuff '
            Return True
    End Function
End Class

Using events that return values are bad, since events can have multiple subscribers and you probably don't know which value will get returned.



回答2:

This is a VB version, these inline functions require framework 4.0 or higher...

    ILoader.evAlloc = Function(rows As Object)
                                 Dim p As Person = TryCast(rows, Person)
                                 If (p.Age And 1) Then
                                     Return 0
                                 Else
                                     Return 1
                                 End If
                             End Function

Sorry didn't read '2010', you need to use;

 yourfunction.yourevent = New eventype(AddressOf yourfunction)

Or use;

 addhandler

http://msdn.microsoft.com/en-us/library/6yyk8z93(v=vs.90).aspx