Share via

Method cannot contain both an 'On Error GoTo' statement and a lambda or query expression

Peter Volz 1,295 Reputation points
2023-08-02T21:44:43.36+00:00

Hello

Once I open my vb .net 2010 project in VS 2022 I get this infamous error here:

On Error Resume Next
Dim MyValueString As String = String.Empty
If PBarX.InvokeRequired Then
    Invoke(Sub() PBarX.Text = MyValueString)
Else
    PBarX.Text = MyValueString
End If

Anyone can help me and advise how to make the code compile without raising this error?

For some reason I insist to keep the On Error Resume Next

I aknowledge the danger of On Error Resume Next :)

Developer technologies | VB
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


2 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,241 Reputation points Microsoft External Staff
    2023-08-03T07:20:35.3966667+00:00

    Hi @Peter Volz ,

    Starting in VS2012, the compiler does not allow methods to contain both "GoTo on error" statements and lambda or query expressions.

    If you don't consider replacing the exception-handling code that uses the "Goto statement on error" with Try... Capture the statement, then you can only compile this code in VS2010.

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Was this answer helpful?

    0 comments No comments

  2. Viorel 127K Reputation points
    2023-08-03T05:46:04.3533333+00:00

    If you insist, then try this:

    On Error Resume Next
    Dim MyValueString As String = String.Empty
    If PBarX.InvokeRequired Then
       PBarX.Invoke(New SetTextDelegate(AddressOf SetText), MyValueString)
    Else
       SetText(MyValueString)
    End If
    
    . . .
    
    Delegate Sub SetTextDelegate(s As String)
    
    Sub SetText(s As String)
       PBarX.Text = s
    End Sub
    

    Also consider this:

    Try
       Dim MyValueString As String = String.Empty
       If PBarX.InvokeRequired Then
          PBarX.Invoke(Sub() PBarX.Text = MyValueString)
       Else
          PBarX.Text = MyValueString
       End If
    Catch
       ' issue hidden
    End Try
    

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.