954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Enter a loop when length is zero

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.

jamesrobb
Newbie Poster
11 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 
if  strFile.Length <> 0
'Code
end if
sonia sardana
Posting Whiz
326 posts since Mar 2008
Reputation Points: 0
Solved Threads: 8
 

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.

jamesrobb
Newbie Poster
11 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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
TomW
Posting Whiz
343 posts since Sep 2009
Reputation Points: 84
Solved Threads: 48
 

You're right. Thanks for your reply.

jamesrobb
Newbie Poster
11 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You