943,971 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 528
  • VB.NET RSS
Oct 9th, 2009
0

Enter a loop when length is zero

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamesrobb is offline Offline
11 posts
since Oct 2007
Oct 9th, 2009
0
Re: Enter a loop when length is zero
VB.NET Syntax (Toggle Plain Text)
  1. if strFile.Length <> 0
  2. 'Code
  3. end if
Reputation Points: 0
Solved Threads: 8
Posting Whiz
sonia sardana is offline Offline
326 posts
since Mar 2008
Oct 9th, 2009
0
Re: Enter a loop when length is zero
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamesrobb is offline Offline
11 posts
since Oct 2007
Oct 10th, 2009
0
Re: Enter a loop when length is zero
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:
VB Syntax (Toggle Plain Text)
  1. Sub Main()
  2.  
  3. Dim strCommittees As String = ""
  4. Dim strFile() As String = Nothing
  5.  
  6. If strFile Is Nothing Then
  7. Console.WriteLine("strFile() is nothing")
  8. Else
  9. Console.WriteLine("strFile.Length : {0}", strFile.Length)
  10. End If
  11.  
  12. Console.WriteLine("'Read blank value into array")
  13. Console.WriteLine("strCommittees.Split(""|""c)")
  14.  
  15. strFile = strCommittees.Split("|"c)
  16. Console.WriteLine("strFile.Length : {0}", strFile.Length)
  17.  
  18. Console.ReadLine()
  19.  
  20. End Sub
Last edited by TomW; Oct 10th, 2009 at 1:05 pm.
Reputation Points: 84
Solved Threads: 48
Posting Whiz
TomW is offline Offline
342 posts
since Sep 2009
Oct 13th, 2009
0
Re: Enter a loop when length is zero
You're right. Thanks for your reply.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jamesrobb is offline Offline
11 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: FV function code help
Next Thread in VB.NET Forum Timeline: Refresh DataGridView





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC