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
The error message is pretty clear:
It's an event, you can't not call it directly. Use a 'RaiseEvent' statement instead:
Your event can't return a value (the C# equivalent can), but you can use a plain delegate instead.
Using events that return values are bad, since events can have multiple subscribers and you probably don't know which value will get returned.
This is a VB version, these inline functions require framework 4.0 or higher...
Sorry didn't read '2010', you need to use;
Or use;
http://msdn.microsoft.com/en-us/library/6yyk8z93(v=vs.90).aspx