473,666 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

for loop is broken?

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). GetElementsByTa gName("li").Ite m(index).InnerT ext)

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?


Oct 15 '08 #1
16 1755
In this kind of loop it is normal

Count - 1

Cor

"Andy B" <a_*****@sbcglo bal.netwrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
>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). GetElementsByTa gName("li").Ite m(index).InnerT ext)

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?

Oct 15 '08 #2
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). GetElementsByTa gName("li").Ite m(index).InnerT ext)

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
Oct 15 '08 #3
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.Doc ument.GetElemen tsByTagName("ul ").Item(0).GetE lementsByTagNam e("li").Count

NotesList.Item s.Add(Me.Browse r.Document.GetE lementsByTagNam e("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

Oct 15 '08 #4
You likely have done something to make this happen. The usual behavior is to
break into the debugger when an exception is thrown...

--
Patrice

"Andy B" <a_*****@sbcglo bal.neta écrit dans le message de groupe de
discussion : Ok************* *@TK2MSFTNGP05. phx.gbl...
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.
>>
Oct 15 '08 #5
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 getan
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).InnerTe xt)
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
Oct 15 '08 #6
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
Oct 15 '08 #7
Andy B wrote:
Is there any other ideas I can do to try and troubleshoot this for
loop?
Do you have Option Strict On at the top of the script? That will point out
where things don't quite match what you intended.

Did you try Göran's tidier-looking version?

Andrew
Oct 15 '08 #8
I have option strict turned on in vs2008sp1 options. I didn't try his code
yet. Was going to and had it copied to the clipboard but lost it in a
computer restart.
"Andrew Morton" <ak*@in-press.co.uk.inv alidwrote in message
news:6l******** ****@mid.indivi dual.net...
Andy B wrote:
>Is there any other ideas I can do to try and troubleshoot this for
loop?

Do you have Option Strict On at the top of the script? That will point out
where things don't quite match what you intended.

Did you try Göran's tidier-looking version?

Andrew

Oct 15 '08 #9
Ok. I tried the following code and it didn't work. Not only does the code
after the next statement fail to run, I get the wrong data inside the
NotesList listbox (well, I get the right data, but it appears twice instead
of only once).

Dim NoteItems as HtmlElementColl ection =
Me.Browser.Docu ment.GetElement sByTagName("ul" ).Item(0).GetEl ementsByTagName ("li")
For index = 0 to NoteItems.Count - 1
NoteList.Items. Add(NoteItems.I tem(index).Inne rText)
Next

Any other ideas?
Is there a way to debug the Browser_Documen tCompleted event code to see
where things are going wrong?
"Andy B" <a_*****@sbcglo bal.netwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>I have option strict turned on in vs2008sp1 options. I didn't try his code
yet. Was going to and had it copied to the clipboard but lost it in a
computer restart.
"Andrew Morton" <ak*@in-press.co.uk.inv alidwrote in message
news:6l******** ****@mid.indivi dual.net...
>Andy B wrote:
>>Is there any other ideas I can do to try and troubleshoot this for
loop?

Do you have Option Strict On at the top of the script? That will point
out where things don't quite match what you intended.

Did you try Göran's tidier-looking version?

Andrew


Oct 15 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2327
by: z0ink | last post by:
I am working on a small graphing application. In the process of graphing I use 3 seperate scripts for getting the job done. The first is the page that the use sees and selects all the data from. This selected data gets passed internally using session variables to the next page. This page takes the passed data, runs it for error checking and determines where to send it next. I have gotten most of the desired results through this method...
21
2061
by: Steven Bethard | last post by:
Can someone point me to the documentation on what's supposed to happen when you use the "for x in X:" syntax when X does not have an __iter__ method? I know that the code: >>> class S: .... def __len__(self): return 42 .... def __getitem__(self, i): return i .... >>> for x in S(): .... print x
32
3805
by: Wenjie | last post by:
Hello, We had a code review with the argument of whether "i" is out of scope as illustrated below: for (int i=0; i<2004; i++) { doSomething(i); }
43
5575
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a fallacy built on the assumption of mythical infinite all powerfull machines. In reality we deal with finite machines that are capable of two states in a loop, they either terminate, or repeat themselves. In the mythical halting problem scenario...
21
18712
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or nanosleep(), depending on platform }
2
2678
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled across some wierd loop behavior. With the pasted code I receive the output that follows. I realize that the code is broken, because the inner loop fails to reset j for each iteration of the outer loop (the fix is commented out). I also know that...
63
3153
by: Aaron Ackerman | last post by:
What is the sytax for exiting a for loop in C#?
0
1498
by: Mark Harrison | last post by:
HOWTO: Integrating Posgresql queries into an event loop. Mark Harrison mh@pixar.com May 27, 2004 Problem ------- The commonly used postgresql APIs will block until completed.
73
4594
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an endless loop in a line with: if a==b: print 'OK' I mean, it would be of much help to me on my way to understanding Python to know how such prefix code leading to an endless loop can look like and if it is eventually not possible to write such...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8871
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.