Results 1 to 15 of 15

Thread: Loop Problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    212

    Loop Problem

    My code is working fine, but when i add a textbox value into be loop, the form does not load anymore. Does anybody know what can be wrong?

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Loop Problem

    You'll have to post the code.

  3. #3
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Loop Problem

    What code?

    EDIT: Too late again.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    212

    Re: Loop Problem

    vb Code:
    1. Imports System.IO
    2.  
    3. Public Class Form1
    4.     Private Function CompareStrings(ByVal x As String, ByVal y As String) As Integer
    5.         Dim xtest = x.Substring(x.IndexOf(")") + 5)
    6.         Dim xtest2 = xtest.Substring(xtest.IndexOf(""))
    7.         Dim strArray1() As String = xtest2.Split(" ")
    8.         Dim xtest3 = strArray1(0)
    9.         Dim ytest = y.Substring(y.IndexOf(")") + 5)
    10.         Dim ytest2 = ytest.Substring(ytest.IndexOf(""))
    11.         Dim strArray2() As String = ytest2.Split(" ")
    12.         Dim ytest3 = strArray2(0)
    13.         Dim numX As Integer = xtest3
    14.         Dim numY As Integer = ytest3
    15.         If Integer.TryParse((numX), numX) AndAlso _
    16.               Integer.TryParse((numY), numY) Then
    17.             Return numY - numX
    18.         Else
    19.             MsgBox("Failed")
    20.             Return 0
    21.         End If
    22.     End Function
    23.  
    24.  
    25.  
    26.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    27.         Dim nwline As String
    28.         Dim strmReader As New StreamReader("C:\Users\Ethan Hayon\Desktop\bssoutput2.txt")
    29.         Do While strmReader.Peek > -1
    30.             nwline = strmReader.ReadLine
    31.             If nwline.Contains("nw") Then
    32.                 ListBox1.Visible = True
    33.                 ListBox1.Items.Add(nwline)
    34.                 Dim count = ListBox1.Items.Count()
    35.                 TextBox2.Text = count
    36.                 Dim Strings As New List(Of String)
    37.                 For Each itm As String In Me.ListBox1.Items
    38.                     Dim myWords As String = Nothing
    39.                     myWords &= itm & Environment.NewLine
    40.                     Strings.Add(itm)
    41.                 Next
    42.                 Strings.Sort(New Comparison(Of String)(AddressOf CompareStrings))
    43.                 Dim nwlinestring = String.Join(Environment.NewLine, Strings.ToArray())
    44.                 TextBox9.Text = nwlinestring
    45.                 Dim line As Integer = 1
    46.                 Do While count <= 1
    47.                     ' Dim strArray() As String = nwlinestring.Split(" "c)
    48.                     ' Dim nwlinestringsplit = strArray(40)
    49.                     ' MsgBox(nwlinestringsplit)
    50.  
    51.  
    52.                 Loop
    53.                 'Do Until line <= count
    54.                 '  TextBox6.Text = nwlinestring
    55.                 'Loop
    56.             End If
    57.         Loop
    58.  
    59.  
    60.         strmReader.Close()
    61.     End Sub
    62. End Class

    thats my code

    look at the "Do While count...." one

    in the loop with three commented out lines, when i define a textbox.text value, the form does not load

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Loop Problem

    Turn Option Strict ON and correct the errors you will get.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    212

    Re: Loop Problem

    is there any other way, my code worked before with no errors
    now i got 30 errors

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Loop Problem

    That is because you have written the code in a bad way. Badly written code can cause problems like the one youre dealing with now.

    Heres an example from your code:

    VB.Net Code:
    1. Dim count = ListBox1.Items.Count()
    2.                 TextBox2.Text = count

    You are declaring the variable count, but you do not specify what type it should be. Then you assign an integer to it. THEN you assign the count variable to TextBox2.Text, which expects a string.
    Just some simple changes makes it all better:

    VB.Net Code:
    1. Dim count As String = Cstr(ListBox1.Items.Count)
    2.                 TextBox2.Text = count
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Loop Problem

    Turning Option Strict Off is like removing the oil light from your dashboard instead of topping up the oil.

    Turn it on, fix the errors, and you'll have much nicer code that will be easier to debug. You should always start any new projects with it on.

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Loop Problem

    Quote Originally Posted by Atheist
    VB.NET Code:
    1. Dim count As String = Cstr(ListBox1.Items.Count)
    2. TextBox2.Text = count
    Simpler still:
    VB.NET Code:
    1. TextBox2.Text = ListBox1.Items.Count.ToString()

    Unless you need them more than once, storing things like string representations of numbers in string variables is just inviting use of them instead of the original numerical variables. Converting to string should always be a one way operation, with the exception of parsing user input.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Loop Problem

    Your code may have worked, but those errors were all real. Most of the errors are probably situations where you were implicitly converting something that now has to be explicitly converted. This can greatly increase speed, but it can also catch lots of subtle bugs. Option Strict will force you to be a better coder, and should be set to On for each and every project. You will have less bugs, faster code, and a better understanding of what you are doing. The cost will be that you have to type slightly more.

    As for the code, here are a few things to consider:

    1) You hold the count of items in the listbox in a textbox. Why not a label? What is the value of having the count be editable (which is all a textbox is good for)?

    2) The same comment might be considered for what you are putting in TextBox9.

    3) I see two sets of three commented out lines. The first set doesn't mention a textbox anywhere in it, and the second set doesn't seem to do anything except fill a textbox over and over. Therefore, I don't see where you are having problems, because neither group of lines seems to match the problem you have described.

    However, in general, the thing to do would be to add a breakpoint somewhere above the area where the problem is (seems like you have identified that the problem occurs in the loop, so add the breakpoint above that). When the execution reaches the breakpoint it will pause. From that point, you can step through the code (F11), stepping over functions that you don't want to go through (F10) if there are any. You can also look at what is in any variable by either putting the mouse over it and seeing the tooltip, or by highlighting the variable and pressing Shift+F9. This will give you a much better idea of what is going wrong.

    Still, you really SHOULD use Option Strict.
    My usual boring signature: Nothing

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Loop Problem

    I knew I'd be too slow.
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    212

    Re: Loop Problem

    okay, i fixed that with option strict on, but i am still having the same problem...

    vb Code:
    1. Option Strict On
    2. Imports System.IO
    3.  
    4. Public Class Form1
    5.     Private Function CompareStrings(ByVal x As String, ByVal y As String) As Integer
    6.         Dim xtest As String = x.Substring(x.IndexOf(")") + 5)
    7.         Dim xtest2 As String = xtest.Substring(xtest.IndexOf(""))
    8.         Dim strArray1() As String = xtest2.Split(" "c)
    9.         Dim xtest3 As String = strArray1(0)
    10.         Dim ytest As String = y.Substring(y.IndexOf(")") + 5)
    11.         Dim ytest2 As String = ytest.Substring(ytest.IndexOf(""))
    12.         Dim strArray2() As String = ytest2.Split(" "c)
    13.         Dim ytest3 As string = strArray2(0)
    14.         Dim numX As Integer = CInt(xtest3)
    15.         Dim numY As Integer = CInt(ytest3)
    16.         If Integer.TryParse(CStr((numX)), numX) AndAlso _
    17.               Integer.TryParse(CStr((numY)), numY) Then
    18.             Return numY - numX
    19.         Else
    20.             MsgBox("Failed")
    21.             Return 0
    22.         End If
    23.     End Function
    24.  
    25.  
    26.  
    27.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    28.         Dim nwline As String
    29.         Dim strmReader As New StreamReader("C:\Users\Ethan Hayon\Desktop\bssoutput2.txt")
    30.         Do While strmReader.Peek > -1
    31.             nwline = strmReader.ReadLine
    32.             If nwline.Contains("nw") Then
    33.                 ListBox1.Visible = True
    34.                 ListBox1.Items.Add(nwline)
    35.                 Dim count As Integer = ListBox1.Items.Count()
    36.                 TextBox2.Text = CStr(count)
    37.                 Dim Strings As New List(Of String)
    38.                 For Each itm As String In Me.ListBox1.Items
    39.                     Dim myWords As String = Nothing
    40.                     myWords &= itm & Environment.NewLine
    41.                     Strings.Add(itm)
    42.                 Next
    43.                 Strings.Sort(New Comparison(Of String)(AddressOf CompareStrings))
    44.                 Dim nwlinestring As String = String.Join(Environment.NewLine, Strings.ToArray())
    45.                 TextBox9.Text = CStr(nwlinestring)
    46.                 Dim line As Integer = 1
    47.                 Do while count <= 10
    48.                     ' Dim strArray() As String = nwlinestring.Split(" "c)
    49.                     ' Dim nwlinestringsplit = strArray(40)
    50.                     ' MsgBox(nwlinestringsplit)
    51.                     TextBox1.Text = "test"
    52.                 Loop
    53.                 'Do Until line <= count
    54.                 '  TextBox6.Text = nwlinestring
    55.                 'Loop
    56.             End If
    57.         Loop
    58.  
    59.  
    60.         strmReader.Close()
    61.     End Sub
    62. End Class
    Last edited by ethanhayon; Jul 2nd, 2007 at 09:56 AM.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    May 2007
    Posts
    212

    Re: Loop Problem

    oh, and by the way, count = 7

  14. #14
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: Loop Problem

    oh, and by the way, count = 7
    It looks as though you would be stuck in a never ending loop. Count is equal to the listitem count, but the system does not know to check again while it is in your loop. Maybe change the do while to an if else statement or change the criteria for the do while or add a statement that updates count in your do statment.

    D
    Last edited by dminder; Jul 2nd, 2007 at 10:13 AM.
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Loop Problem

    dminder is right. If you do the breakpoint thing and step through the loop, you will see that since count remains always less than 10 in the loop, then the loop never finishes.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width