Results 1 to 2 of 2

Thread: [RESOLVED] [2005] While... Loop (Real Problem this time)

  1. #1

    Thread Starter
    Lively Member smart_phil_dude1's Avatar
    Join Date
    Jun 2005
    Location
    Behind You!
    Posts
    125

    Resolved [RESOLVED] [2005] While... Loop (Real Problem this time)

    I'm using a while loop and it works fine, but it uses an integer variable and at the end I made it increase by 1 each time it loops. the problem is that, the variable is used in the loop but it seems like the variable doesn't go up.

    ex:
    VB Code:
    1. Dim intLineNumber As Integer = 1
    2.  
    3. Private Sub Button2_Click()
    4.     While intLineNumber <= 5
    5.         Log1.Text = Log1.Text & vbNewLine & "Changing " & Form1.TextBox1.Lines.GetValue(intLineNumber - 1) & "..."
    6.         intLineNumber = intLineNumber + 1
    7.     End While
    8. End Sub

    "intLineNumber = intLineNumber + 1" has an effect on the While... Loop, as it doesn't continue infinitely, but it has no affect on the GetValue. What's the problem?


    Thanks,
    Phil.
    Last edited by smart_phil_dude1; Jan 24th, 2007 at 05:29 PM.
    Please Help Us Save Ana or donate now by going to Oneana.com

    Visit my Website at http://www.therealfantasy.com

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] While... Loop (Real Problem this time)

    That is a job for a For loop, not a While loop:
    VB Code:
    1. Dim lines As String() = Form1.TextBox1.Lines
    2.  
    3. For i As Integer = 0 to 4 Step 1
    4.     Log1.AppendText(String.Format("Changing {0}...", lines(i)))
    5. Next i
    Incrementing a variable each iteration is what For loops do, so they should almost always be used when that is what's controlling the loop. Do and Wjile loops would normally be used for less regular conditions, which will change from True to False or vice versa with less predictability. An example is reading a file line by line. You don't know how many lines there will be so a For loop is impracticle. You would keep reading WHILE there is data to read, so a While loop is most practical.

    Also, notice that I do NOT reference the Lines property of the TextBox over and over, but only once. That's because each time you get the value of the Lines property it creates a new array object. The data isn't going to change so it is much more efficient to use the same array object each time.

    As for the actual issue you mentioned, that's what debugging is for. Place a break point at the top of the code and use F10 to step through it line by line. At each step you can test any variable, property or expression you like to see what values are where and whether they are what you expect them to be.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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