Hi,
Can anyone explain why my code enters a loop when the length of a file is zero. I would expect that the loop below would not be entered if strFile.Length is zero.

strFile = strSubCommittees.Split("|")
For i = 0 To strFile.Length - 1

Next i

Thanks.

Dani AI

Generated

A short clarification and a couple of practical fixes for the behavior seen in this thread.

The Split call on an empty string produces an array with one element: the empty string. That is why the loop still runs once. This is the point explained; ’s suggestion to guard by checking the array length will work, but there are slightly cleaner options.

Recommended approaches:

  • Avoid splitting an empty source string. Test the source with String.IsNullOrWhiteSpace and skip processing if it’s blank.
  • Use StringSplitOptions.RemoveEmptyEntries when splitting so empty parts are not returned, and iterate with For Each (which is clearer than manual index math). Example pattern in VB:
Dim source As String = strSubCommittees
Dim parts() As String = source.Split(New Char() {"|"c}, StringSplitOptions.RemoveEmptyEntries)

If parts.Length > 0 Then
    For Each part As String In parts
        Dim item = part.Trim()
        ' process item
    Next
End If

Notes and cautions:

  • An array variable that was never assigned is Nothing; an assigned-but-empty source string yields a one-element array containing "". Check Is Nothing only if the array may be uninitialized.
  • Trimming and validating each part prevents whitespace-only entries from being treated as meaningful data.
  • For older frameworks without the StringSplitOptions overload, check the source string first and then split.

See Microsoft docs for the Split overloads and StringSplitOptions for details: String.Split documentation and StringSplitOptions.

Acknowledgement: behavior noted by ; guard suggestion from ; original report from .

Recommended Answers

All 4 Replies

if  strFile.Length <> 0
'Code
end if

Yes. I thought of that but I wouldn't of thought you had to as it shouldn't enter the loop if the length is zero. Anyway, thanks for your reply, I guess I'll have to add the extra code.
Thanks.

Your array is not actually zero length, it has a length of one!

strFile is an array. The array can either be "nothing" or have at least one element to it; even if it is blank.
strFile = strSubCommittees.Split("|")
This will automatically assign at least one element to your array, even if it is blank and doesnt actually have a value in that element.

Try this in a console app to see the results:

Sub Main()

        Dim strCommittees As String = ""
        Dim strFile() As String = Nothing

        If strFile Is Nothing Then
            Console.WriteLine("strFile() is nothing")
        Else
            Console.WriteLine("strFile.Length : {0}", strFile.Length)
        End If

        Console.WriteLine("'Read blank value into array")
        Console.WriteLine("strCommittees.Split(""|""c)")

        strFile = strCommittees.Split("|"c)
        Console.WriteLine("strFile.Length : {0}", strFile.Length)

        Console.ReadLine()

    End Sub

You're right. Thanks for your reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.