Enter a loop when length is zero

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 9
Reputation: jamesrobb is an unknown quantity at this point 
Solved Threads: 0
jamesrobb jamesrobb is offline Offline
Newbie Poster

Enter a loop when length is zero

 
0
  #1
Oct 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz
 
0
  #2
Oct 9th, 2009
  1. if strFile.Length <> 0
  2. 'Code
  3. end if
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 9
Reputation: jamesrobb is an unknown quantity at this point 
Solved Threads: 0
jamesrobb jamesrobb is offline Offline
Newbie Poster
 
0
  #3
Oct 9th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 321
Reputation: TomW is on a distinguished road 
Solved Threads: 45
TomW TomW is offline Offline
Posting Whiz
 
0
  #4
Oct 10th, 2009
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 9
Reputation: jamesrobb is an unknown quantity at this point 
Solved Threads: 0
jamesrobb jamesrobb is offline Offline
Newbie Poster
 
0
  #5
Oct 13th, 2009
You're right. Thanks for your reply.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 315 | Replies: 4
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC