Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nothing collection in for each loop - how to handle it?

Tags:

vb.net

How do I handle a for each loop when the collection is nothing, I thought it would just skip over but i get an exeption?

Do I need to wrap the foreach loop in a if to check for nothing and only if it is not nothing then enter in the for each loop?

For Each item As String In MyStringList 

    'do something with each item but something myStringList will be nothing?


Next
like image 664
Hello-World Avatar asked Jul 18 '12 17:07

Hello-World


2 Answers

Do I need to wrap the foreach loop in a if to check for nothing and only if it is not nothing then enter in the for each loop?

Yes.

If MyStringList IsNot Nothing Then
    For Each item As String In MyStringList 
       'do something ...
    Next
End If

Microsoft says it is by design:

I think that most foreach loops are written with the intent of iterating a non-null collection. If you try iterating through null you should get your exception, so that you can fix your code. Foreach is basically a syntactic convenience. As such, it should not be "magical" and do unexpected things under the hood. I agree with the post that proposed the use of empty collections rather than null. (They can typically be reused quite a bit using singleton techniques).

like image 84
Tim Schmelter Avatar answered Nov 15 '22 08:11

Tim Schmelter


Adding the If collection IsNot Nothing Then is not that onerous, but if you do have this construct a lot, this Extension method may be preferable:

    '''---------------------------------------------------------------------
    ''' Extension Function: OrEmpty
    ''' 
    ''' <summary>
    '''   Returns an empty enumeration if the source is Nothing.
    ''' </summary>
    ''' 
    ''' <typeparam name="T">The type to create an enumeration of. Normally inferred.</typeparam>
    ''' 
    ''' <param name="Source">The source enumeration.</param>
    ''' 
    ''' <returns>The source enumeration unless it is Nothing; then an empty enumeration.</returns>
    ''' 
    ''' <remarks>
    ''' </remarks>
    ''' 
    ''' <revisionhistory>
    '''   100930 MEH Created.
    ''' </revisionhistory>
    '''---------------------------------------------------------------------
    <Extension()> _
    Function OrEmpty(Of T)(ByVal Source As IEnumerable(Of T)) As IEnumerable(Of T)
        If Source IsNot Nothing Then _
            Return Source
        Return Enumerable.Empty(Of T)()
    End Function

And with Option Infer On you don't need to specify the type, so the example use is just:

For Each item As String In MyStringList.OrEmpty 

    'do something with each item but something myStringList will be nothing?

Next
like image 34
Mark Hurd Avatar answered Nov 15 '22 07:11

Mark Hurd