如何将此C#事件/委托代码转换为VB.NET(2008或2010) [英] How to Convert This C# Event/Delegate Code Into VB.NET (2008 or 2010)
问题描述
从C#到VB.NET这是相关的C#代码
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;
}
}
}
这就是我的想法到目前为止。
C#委托是一个返回布尔结果的函数。因此,我将委托定义和事件声明转换如下。
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返回了错误消息:事件不能被声明为具有返回类型的委托类型。
Visual Studio Returned with the Error Message: "Events cannot be declared with a delegate type that has a return type."
因此,我将其改为SUB并完成了这种翻译*。
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
*注意:我已经意识到我无法检查 Event对象什么都不是,只是对于VB内部委托对象* EVENTNAME * Event。
*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.
但是对于第二部分,我不知道该怎么做用它做。我收到以下错误消息:
公共事件evAlloc(以整数形式的param1)是一个事件,不能直接调用。使用'RaiseEvent'语句引发事件。
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.
有什么想法吗?!谢谢
推荐答案
错误消息非常清楚:
公共事件evAlloc(作为整数的param1)是一个事件,不能直接调用。使用'RaiseEvent'语句引发事件。
Public Event evAlloc (param1 As Integer)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
这是一个事件,您不能直接调用它。改用'RaiseEvent'语句:
It's an event, you can't not call it directly. Use a 'RaiseEvent' statement instead:
RaiseEvent evAlloc(1)
您的事件无法返回值(C#等效项可以返回),但是您可以使用普通委托。
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.
这篇关于如何将此C#事件/委托代码转换为VB.NET(2008或2010)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!