Search code examples
vb.netgotocontrol-flow

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


Goto cannot go to another procedure.

If a condition is true in the called procedure, then I need the calling procedure to skip to the end of the calling procedure.

The Problem:

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

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

My solution is to set a variable and then use that in combination with an if statement, but that also means I need to make the variable public.

Ok so here is the actual code:

Without any solution:

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

With my Solution:

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

This just seems like alot of extra code to solve a simple problem. Is there a better way?


Solution

  • What about using the return value to tell the outer function to skip the other stuff?

    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