2

The code below is part of project to save exceptions globally. I have converted this from C# to VB with both SharpDevelop and with Telerik's Code Converter, and I get identical results.

Searching through Stackoverflow, I found this question: "'Why won't this C# convert to VB?', which address the same error I have and shows a simple answer, but doesn't show how to use it - at least so I would know what to change.

What I'm getting is an error on 'ONWRITETODATABASE'. The complete error is: 'Public Shared Event OnWriteToDatabase(type As String, text As String)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

What do I change to make this work? (The caps are mine.)

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
DanW52
  • 67
  • 7

2 Answers2

3

If you really want to include the "IsNot Nothing" check (and note Ben N's observation that you don't need it), you can reference the hidden VB event field:

If OnWriteToDatabaseEvent IsNot Nothing Then 'the event followed by "Event"
Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28
  • Dave & @Ben N: the procedure is trying to write error information to a database. If it fails then it will write error information to a local file (last resort and harder to get to). So I do need the IsNot Nothing check. I really don't know what you mean by 'the event followed by "Event". How would you write this out? – DanW52 Apr 29 '14 at 00:16
  • It's written exactly as I've specified: "If OnWriteToDatabaseEvent IsNot Nothing Then". (VB adds a hidden field named the same as the event followed by 'Event' - you normally don't use it, but it fits your requirement). – Dave Doknjas Apr 29 '14 at 00:26
  • Hi Dave - thanks for your response. However, I've only been using .Net for about year now, and obviously your skill is way beyond mine. I truly do not know what you mean here. Could you create an answer and rewrite this code so that the error is removed? Once I see what you're doing, then I'll be in a better position to learn how it works. Thanks! – DanW52 Apr 29 '14 at 12:44
  • All I'm suggesting is that instead of "If ONWRITETODATABASE IsNot Nothing Then", use "If OnWriteToDatabaseEvent IsNot Nothing Then". – Dave Doknjas Apr 29 '14 at 14:24
  • Thanks Dave! That did it - now I can search to learn more about how this works. – DanW52 Apr 29 '14 at 19:37
  • I think the hidden event field in VB is kind of obscure, but I'm sure that Microsoft documents it somewhere. – Dave Doknjas Apr 29 '14 at 19:52
0

First up, events in VB are not handler collections; they're just something you can listen to with AddHandler. Therefore, it's not possible to know (with the simple translation given) whether anybody heard your raising of the event. Also, As clauses are not valid after events. What I think you should do is replace the first two lines with this:

Public Shared Event OnWriteToDatabase(DataType As String, Text As String)

Then, remove the null check and the _logToDatabaseOK stuff. (While you're at it, change the Function into a Sub, getting rid of the return.

Ben N
  • 2,883
  • 4
  • 26
  • 49