 |
 |

07-28-2012, 02:58 PM
|
 |
Newcomer
|
|
Join Date: Jun 2011
Location: Northern Ireland
Posts: 15
|
|
Trouble with for loop not ending
|
Hi, I am building a form that will load a list of races from a text file (one race per line) into separate items in a listbox. I can get the form to complete this okay, it will load all of the races into the listbox, however the for loop that adds items to the listbox does not complete. I added a msgbox after the next at the end of the loop, but the msgbox never appears.
First, i use a streamreader to load count the number of lines in the textbox, i use this value to redim an array to which i then load each race name. After this, i use the strings in the array within the loop that is not ending, to add each race name (string) to the listbox as an item. Here is my code below:
Code:
Public Class SessionRaceDefinition
Public races() As String
Private Sub raceload()
Dim filein As New StreamReader(My.Settings.RacesPath)
Dim strData As String = ""
Dim lngCount As Long = 1
Dim counter As Integer = 0
While (Not (filein.EndOfStream))
strData = filein.ReadLine()
lngCount = lngCount + 1
End While
filein.Close()
Dim filein2 As New StreamReader(My.Settings.RacesPath)
ReDim races(lngCount)
MsgBox("Done 1!")
While (Not (filein2.EndOfStream))
strData = filein2.ReadLine()
races(counter) = strData
counter = counter + 1
End While
MsgBox("Done 2!")
'Everything is okay until this section
LBRaces.Items.Clear()
For x As Integer = LBound(races) To UBound(races)
LBRaces.Items.Add(races(x))
Next
MsgBox("Done 3!")
End Sub
I am certain it is loading all lines from the text file as they are all in the listbox and i made sure the last line in the text file is not blank so as to throw any errors.
If you are wondering why i loaded 2 streamreaders that are the same, it is because after i used the first one to count the lines, i was not sure how to return to the first line to start reading from the lines, so i disposed of the initial streamreader and loaded an identical one.
Any help would be greatly appreciated, Many Many Thanks in advance.
|
__________________
The third-rate mind is only happy when it is thinking with the majority.
The second-rate mind is only happy when it is thinking with the minority.
The first-rate mind is only happy when it is thinking
|

07-28-2012, 04:30 PM
|
 |
Fabulous Florist
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,415
|
|
|
There are two possible reasons why the code might terminate without you getting an exception:
1) There's a Try/Catch block eating the error.
2) It's in the Load handler and you're falling victim to a well-known bug.
Are either of these true?
|
|

07-28-2012, 05:02 PM
|
 |
Newcomer
|
|
Join Date: Jun 2011
Location: Northern Ireland
Posts: 15
|
|
|
could you explain what is meant by the load handler? and no, there is no try catch in my code.
|
__________________
The third-rate mind is only happy when it is thinking with the majority.
The second-rate mind is only happy when it is thinking with the minority.
The first-rate mind is only happy when it is thinking
|

07-29-2012, 04:58 AM
|
 |
Contributor
|
|
Join Date: Oct 2003
Location: Pennsylvania
Posts: 422
|
|
Code:
Public Class SessionRaceDefinition
Public races() As String
Private Sub raceload()
Dim filein As New StreamReader(My.Settings.RacesPath)
Dim strData As String = ""
'Dim lngCount As Long = 1
'Dim counter As Integer = 0
Dim lngCount As Long = 1
Dim counter As Long = 0
Dim x as Long
While (Not (filein.EndOfStream))
strData = filein.ReadLine()
lngCount = lngCount + 1
End While
filein.Close()
Dim filein2 As New StreamReader(My.Settings.RacesPath)
ReDim races(lngCount)
MsgBox("Done 1!")
While (Not (filein2.EndOfStream))
strData = filein2.ReadLine()
races(counter) = strData
counter = counter + 1
End While
MsgBox("Done 2!")
'Everything is okay until this section
LBRaces.Items.Clear()
For x = 0 to counter -1
'For x As Integer = LBound(races) To UBound(races)
LBRaces.Items.Add(races(x))
Next
MsgBox("Done 3!")
End Sub
|
|

07-29-2012, 05:14 AM
|
 |
Senior Contributor
* Expert *
|
|
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,272
|
|
Longs in .Net are 64 bit, not 32 bit like they were in VB6. Integers are now 32bit and should be used instead of Longs.
You can get your data into an array much easier than you do but splitting the contents of the file. Always worth error catching on file handling too.
Code:
Dim sr as StreamReader
Dim file as string
Dim lines() as string
Try
sr = New StreamReader(My.Settings.RacesPath)
' Read the whole lot
file = sr.ReadtoEnd
Catch ex as exception
messagebox.show(ex.message, "Error reading file")
' What happens after the error is up to you. You might exit initialising, returning False
' so the caller knows it's gone wrong
Return False
Finally
' Closes the file regardless of error or not
if sr isnot nothing then
sr.close
end if
End Catch
' Put the file contents into an array as long as it read the file OK.
lines = split(file, vbcrlf)
' Clear the memory taken up by the file if no longer needed
file = ""
' You now have your races array in lines
' You can For Each over arrays in .Net
Dim race as string
LBRaces.Items.Clear()
For Each race in lines
LBRaces.Items.Add(race)
Next
|
__________________
There are no computers in heaven!
|

07-30-2012, 08:00 AM
|
 |
Fabulous Florist
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,415
|
|
|
"load handler" means "the code that is run in response to the Form.Load event".
|
|

07-30-2012, 03:25 PM
|
 |
Newcomer
|
|
Join Date: Jun 2011
Location: Northern Ireland
Posts: 15
|
|
I changed the code to the example given by DrPunk above, and inserted it into the load handler directly. I then inserted msgbox's at multiple places to find where the code was ending. The code is not completing the split. I have no idea why. Thank you both for your help, much appreciated 
|
__________________
The third-rate mind is only happy when it is thinking with the majority.
The second-rate mind is only happy when it is thinking with the minority.
The first-rate mind is only happy when it is thinking
|

07-30-2012, 05:48 PM
|
 |
Fabulous Florist
Forum Leader * Guru *
|
|
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,415
|
|
Put it somewhere that's not the Load handler. There is a known issue with 64-bit machines where any exception thrown by the Load handler disappears into the abyss, leaving you wondering why the devil your code didn't work but didn't throw an exception. Put it in a button handler or something, click the button, and when it explodes you'll have your answer
Otherwise you have to know everything that could go wrong on the line of code that's throwing the uncatchable exception, then test everything until it works. Much harder.
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|