Home > database >  How to use Continue For within an inner function in VB.NET
How to use Continue For within an inner function in VB.NET

Time:09-22

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?

CodePudding user response:

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
  • Related