Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Trouble with for loop not ending


Reply
 
Thread Tools Display Modes
  #1  
Old 07-28-2012, 02:58 PM
wilson208's Avatar
wilson208 wilson208 is offline
Newcomer
 
Join Date: Jun 2011
Location: Northern Ireland
Posts: 15
Default 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.
Attached Images
File Type: jpg form.jpg (39.9 KB, 5 views)
__________________
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
Reply With Quote
  #2  
Old 07-28-2012, 04:30 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,415
Default

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?
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 07-28-2012, 05:02 PM
wilson208's Avatar
wilson208 wilson208 is offline
Newcomer
 
Join Date: Jun 2011
Location: Northern Ireland
Posts: 15
Default

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
Reply With Quote
  #4  
Old 07-29-2012, 04:58 AM
TRSDOSBasic79's Avatar
TRSDOSBasic79 TRSDOSBasic79 is offline
Contributor
 
Join Date: Oct 2003
Location: Pennsylvania
Posts: 422
Default

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
Reply With Quote
  #5  
Old 07-29-2012, 05:14 AM
DrPunk's Avatar
DrPunk DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,272
Default

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!
Reply With Quote
  #6  
Old 07-30-2012, 08:00 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,415
Default

"load handler" means "the code that is run in response to the Form.Load event".
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #7  
Old 07-30-2012, 03:25 PM
wilson208's Avatar
wilson208 wilson208 is offline
Newcomer
 
Join Date: Jun 2011
Location: Northern Ireland
Posts: 15
Default

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
Reply With Quote
  #8  
Old 07-30-2012, 05:48 PM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,415
Default

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.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply

Tags
continuous for loop, listbox


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->