在 visual basic 中是否有比多个 sub 更好的 Goto 等价物?

Is there a better Equivalent of Goto over multiple subs in visual basic?

Goto 无法转到另一个过程。

如果被调用过程中的条件为真,那么我需要调用过程跳到调用过程的末尾。

问题:

 Sub Outer()
    Inner1()
    Inner2()
    Inner3()
  thisline:
  End Sub

  Sub Inner2()
      If condition is true
        goto thisline
      else dostuff
        end if
  End sub

我的解决方案是设置一个变量,然后将其与 if 语句结合使用,但这也意味着我需要使变量 public.

好的,下面是实际代码:

无解:

Sub btnRun_Click() Handles btnRun.Click
    ChooseSaveLocation()
    Case_DatatoGet(DatatoGet:=cmb_DatatoRetrieve.Text)

    WriteDatatoDestination(datachoice:=cmb_DatatoRetrieve.SelectedItem, 
                                 Destination:=cmb_Destination.SelectedItem)
    Debug.Print("---------------Finished with Run----------------")

End Sub

Sub Case_DatatoGet(DatatoGet)
    If DatatoGet = "" Then
        MsgBox("Please click on settings and choose data to retrieve")

    Else
        Select Case DatatoGet
            Case "Company Info"
                GetCompanyInfo(txt_APIUsername.Text, txt_APIPassword.Text)
            Case "Prices"
                GetPrices(txt_APIUsername.Text, txt_APIPassword.Text)
            Case "Balance Sheets"
                Debug.Print("Write a balance sheet sub")
            Case "Income Statements"
                Debug.Print("Write a Income Statement sub")
            Case "Statement of Cash Flows"
                Debug.Print("Write a Statement of Cash Flows sub")
        End Select
    End If

End Sub

我的解决方案:

Public ContinueVar
...
Sub btnRun_Click() Handles btnRun.Click
    ContinueVar = True
    ChooseSaveLocation()
    Case_DatatoGet(DatatoGet:=cmb_DatatoRetrieve.Text)
    If ContinueVar = True Then
        WriteDatatoDestination(datachoice:=cmb_DatatoRetrieve.SelectedItem, 
                                 Destination:=cmb_Destination.SelectedItem)
        Debug.Print("---------------Finished with Run----------------")
    End If

End Sub

Sub Case_DatatoGet(DatatoGet)
    If DatatoGet = "" Then
        MsgBox("Please click on settings and choose data to retrieve")
        ContinueVar = False
    Else
        Select Case DatatoGet
            Case "Company Info"
                GetCompanyInfo(txt_APIUsername.Text, txt_APIPassword.Text)
            Case "Prices"
                GetPrices(txt_APIUsername.Text, txt_APIPassword.Text)
            Case "Balance Sheets"
                Debug.Print("Write a balance sheet sub")
            Case "Income Statements"
                Debug.Print("Write a Income Statement sub")
            Case "Statement of Cash Flows"
                Debug.Print("Write a Statement of Cash Flows sub")
        End Select
    End If

End Sub

这看起来像是解决一个简单问题的大量额外代码。 有没有更好的方法?

使用 return 值告诉外部函数跳过其他内容怎么样?

Sub Outer()
    Inner1()
    If Inner2() = True Then
        Inner3()
    End If

End Sub

Function Inner2() As Boolean
    If condition Is True Then
        Return False
    Else
        dostuff()
        Return True
    End If

End Function