Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading