c# - 转换为VB后, 'is an event and cannot be called directly.'

标签 c# vb.net raiseevent

下面的代码是全局保存异常项目的一部分。我已经使用 SharpDevelop 和 Telerik 的代码转换器将其从 C# 转换为 VB,并且得到了相同的结果。

通过 Stackoverflow 搜索,我发现了这个问题:“‘Why won't this C# conversion to VB?’,它解决了我遇到的相同错误并显示了一个简单的答案,但没有显示如何使用它 - 在至少这样我就知道要改变什么。

我收到的是“ONWRITETODATABASE”上的错误。完整的错误是:“公共(public)共享事件OnWriteToDatabase(type As String,text As String)”是一个事件,不能直接调用。使用“RaiseEvent”语句引发事件。

我需要改变什么才能使这项工作正常进行? (帽子是我的。)

Public Delegate Sub DatabaseWriteEventHandler(type As String, text As String)
Public Shared Event OnWriteToDatabase As DatabaseWriteEventHandler

Protected Function OnWriteToDatabase() As Boolean

    _logToDatabaseOK = False

    If ONWRITETODATABASE IsNot Nothing Then
        Try
            RaiseEvent OnWriteToDatabase(_exceptionType, _exceptionText)
            _logToDatabaseOK = True
        Catch ex As Exception
            _results.Add("LogToDatabase", ex.ToString())
        End Try
    Else
        _results.Add("LogToDatabase", "No subscriptions to OnWriteToDatabase event")
    End If

    Return _logToDatabaseOK

End Function

最佳答案

如果您确实想要包含“IsNot Nothing”检查(请注意 Ben N 的观察表明您不需要它),您可以引用隐藏的 VB 事件字段:

If OnWriteToDatabaseEvent IsNot Nothing Then 'the event followed by "Event"

关于c# - 转换为VB后, 'is an event and cannot be called directly.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23351978/

相关文章:

c# - DLL导入: Unable to find Entry Point "fnMultiply" in DLL "ImportDLL"

c# - 为什么 DisplayMember 不适用于 ListBox 中手动添加的 DataRow?

php - 如何在php中调用带有参数值的http post

events - 将 c# 转换为 vb.net 'RaiseEvent' 语句以引发事件以使用 Gzip

vb.net - 如何在VB.NET代码中触发事件?

c# - MongoDB C# Driver 2.1.0 - 解析引用

Javascript 函数检查字符串是否为有效日期

vb.net - 从 Windows 窗体应用程序运行 sql 脚本

mysql - VB.NET 和 MySQL 查询