I tried the idea with the try..catch block with the Count-1 idea. This is
what I get:
1. No exceptions are thrown in the code anywhere.
2. When using next index, the for loop works, but none of the code after the
for loop runs.
3. When just using the next statement, the for loop messes up things and I
get the wrong data as well as the code after next statement wont run.
4. The Count - 1 statement doesn't seem to make any changes.
5. Running the entire program through the debugger, index evaluates to the
right value.
6. Stepping into the code, anything after the next statement fails (it is
totally skipped).
7. The code after the next statement doesn't appear to be broken since
MessageBox.Show ("succeeded" ) doesn't even run.
Is there any other ideas I can do to try and troubleshoot this for loop?
"kimiraikko nen" <ki************ *@gmail.comwrot e in message
news:f1******** *************** ***********@o4g 2000pra.googleg roups.com...
On Oct 15, 1:38 pm, "Andy B" <a_bo...@sbcglo bal.netwrote:
OK. Figured I had to be missing something. Will try it out. On the note of
try...catch blocks, how do you catch all exceptions and show them in a
messageBox (for debugging reasons. When I run the code below, I never get
an
exception raised or anything unless the system just throws it away and I
don't know it.
"Göran Andersson" <gu...@guffa.co mwrote in message
news:eG******** ******@TK2MSFTN GP05.phx.gbl...
Andy B wrote:
I have the following code inside of a WebBrowser.Docu mentCompleted
event:
For index As Integer = 0 To
Me.Browser.Docu ment.GetElement sByTagName("ul" ).Item(0).GetEl ementsByTagName *("li").Count
NotesList.Items .Add(Me.Browser .Document.GetEl ementsByTagName ("ul").Item(0). *GetElementsByT agName("li").It em(index).Inner Text)
Next index
For some reason, nothing after the next index will run. It's like
everything after it gets ignored. Even when I just use next it doesn't
work (although I get the wrong data if I do that). How do you fix this?
I suppose that you are catching exceptions somewhere?
In the last iteration of the loop, you are trying to access an item that
is outside the list, so you get an IndexOutOfRange Exception, which will
send the execution of the code into the catch block, skipping the code
after the loop.
You should NEVER just catch an exception and silently throw it away
(unless in a very very small try...catch block where you specify exactly
what exception you expect, and know exactly why you can ignore it).
The index specification in the Next statement is optional, and excluding
it doesn't change the behaviour of the loop in any way.
You should loop from 0 to Count-1, not from 0 to Count:
Dim items as HtmlElementColl ection =
Me.Browser.Docu ment.GetElement sByTagName("ul" ).Item(0).GetEl ementsByTagName *("li")
For index = 0 to items.Count - 1
NoteList.Items. Add(items.Item( index).InnerTex t)
Next
--
Göran Andersson
_____
http://www.guffa.com- Hide quoted text -
- Show quoted text -
Put your code in a try-catch block to test if it returns any
exception:
'-------------------------------------
Try
For index As Integer = 0 To
Me.Browser.Docu ment.GetElement sByTagName("ul" ).Item(0).GetEl ementsByTagName *
("li").Count - 1
NotesList.Items .Add(Me.Browser .Document.GetEl ementsByTagName ("ul").Item(0). *
GetElementsByTa gName("li").Ite m(index).InnerT ext)
Next index
' Let's try to catch exception if so,
'Use msgbox to see
Catch ex As Exception
Msgbox(ex.Messa ge)
End Try
'-----------------------------------
In my idea, as you describe in your first post, if you were using your
loop in a try-catch block, you may have got an exception inside your
for-next loop, thus the execution in for-next loop is aborted and an
exception may have been thrown. And you may not have seen the
exception if your Catch block was empty (the worst scenerio). To
catch, use it as above.
Onur Güzel