0

I have an outer function which contains a loop and an inner function where I'm trying to use Continue For. How can I use the Continue For within the inner function. For example:

Public Function Create(variable1 As String, variable2 As String) As String
   Dim settingsList = 'Code to store data in list
   For Each setting In settingsList
      ProcessSomething(variable1, variable2)
   Next
Return ""
End Function

Private Function ProcessSomething(variable1 As String, variable2 As String)  As String
   If variable1 = "" Then
      'Log Error
      Continue For
   End If
Return ""
End Function

The Private function is called within the Loop of the Public Function, but I see a warning in Visual Studio that says 'Continue For' can only appear inside a 'For' statement. How would you use a Continue For in a case like this or is not possible and not even necessary?

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
Rich
  • 6,470
  • 15
  • 32
  • 53
  • 2
    Unless there's code after the call to `ProcessSomething` that you want skipped, then just `Return`ing (perhaps `Nothing`) early from `ProcessSomething` will have the same effect of moving to the next loop iteration. – Lance U. Matthews Sep 21 '22 at 14:51

1 Answers1

1

You can't use the Continue For outside a For, as the error is telling you. You're trying to use it in an independent function which may or may not be called from within a loop, so the compiler can't accept your usage of it in case you call the function in a different context.

Instead, make the function return a result, and use that to decide whether to execute Continue For or not.

For example:

Public Function Create(variable1 As String, variable2 As String) As String
   Dim settingsList = 'Code to store data in list
   For Each setting In settingsList
      If ProcessSomething(variable1, variable2) = True Then
        Continue For
      End If
   Next
Return ""
End Function

Private Function ProcessSomething(variable1 As String, variable2 As String)  As Boolean
   If variable1 = "" Then
      'Log Error
      Return True
   Else
     Return False
   End If
End Function
ADyson
  • 57,178
  • 14
  • 51
  • 63