|
-
Jan 24th, 2007, 05:12 PM
#1
Thread Starter
Lively Member
[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:
Dim intLineNumber As Integer = 1
Private Sub Button2_Click()
While intLineNumber <= 5
Log1.Text = Log1.Text & vbNewLine & "Changing " & Form1.TextBox1.Lines.GetValue(intLineNumber - 1) & "..."
intLineNumber = intLineNumber + 1
End While
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.
-
Jan 24th, 2007, 05:35 PM
#2
Re: [2005] While... Loop (Real Problem this time)
That is a job for a For loop, not a While loop:
VB Code:
Dim lines As String() = Form1.TextBox1.Lines
For i As Integer = 0 to 4 Step 1
Log1.AppendText(String.Format("Changing {0}...", lines(i)))
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|