for循环被打破了? [英] for loop is broken?

查看:94
本文介绍了for循环被打破了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WebBrowser.DocumentCompleted事件中有以下代码:


索引As Integer = 0 To

Me.Browser.Document.GetElementsByTagName (< ul")。Ite m(0)。GetElementsByTagName(" li")。Count


NotesList.Items.Add(Me.Browser.Document.GetElement sByTagName(") ; ul")。Item(0).GetElementsByTagName(" li")。Item(index).InnerText)


下一个索引


由于某种原因,下一个索引之后不会运行任何内容。这就像被忽略之后的所有东西一样

。即使我只是使用下一个它也不起作用(尽管如果我这样做,我会获得错误的数据)。你是怎么解决这个问题的?


解决方案

在这种循环中它是正常的

点数 - 1


Cor


" Andy B" < a _ ***** @ sbcglobal.netwrote in message

news:%2 **************** @ TK2MSFTNGP05.phx.gbl .. 。


>我在WebBrowser.DocumentCompleted事件中有以下代码:


索引As Integer = 0至
Me.Browser.Document.GetElementsByTagName(" ul")。Ite m(0)。GetElementsByTagName(" li")。Count


NotesList.Items.Add(Me.Browser.Document.GetElement sByTagName(" ul")。Item(0).GetElementsByTagName(" li")。Item(index).InnerText)


下一个索引


出于某种原因,下一个索引之后不会运行任何内容。这就像

被忽略之后的一切。即使我刚刚使用下一步它也没有工作(虽然如果我这样做,我会得到错误的数据)。你如何解决这个问题?


Andy B写道:


I在WebBrowser.DocumentCompleted事件中包含以下代码:


索引As Integer = 0 To

Me.Browser.Document.GetElementsByTagName(" ul" ;)。Ite m(0)。GetElementsByTagName(" li")。Count


NotesList.Items.Add(Me.Browser.Document.GetElement sByTagName(" ul") .Item(0)。GetElementsByTagName(" li")。Item(index).InnerText)


下一个索引


由于某种原因,下一个索引将运行后没有任何内容。这就像被忽略之后的所有东西一样

。即使我只是使用下一个它也不起作用(尽管如果我这样做,我会获得错误的数据)。你如何解决这个问题?



我想你在某个地方捕捉异常?


在循环的最后一次迭代中,你试图访问一个

在列表之外的项目,所以你得到一个IndexOutOfRangeException,它将把b / b
的代码执行发送到catch块中,跳过循环后代码




你永远不应该只是抓住一个异常并默默地扔掉它

(除非是非常小的尝试... catch块你指定的确是

你期望什么例外,并且确切地知道为什么你可以忽略它。)


索引Next语句中的规范是可选的,并且不包括

它不会以任何方式改变循环的行为。


你应该从0循环到Count-1,而不是从0到Count:


将项目设为HtmlElementCollection =

Me.Browser.Document.GetElementsByTagName(" ul")。它em(0)。GetElementsByTagName(" li")

索引= 0到items.Count - 1

NoteList.Items.Add(items.Item(index) .InnerText)

下一页


-

G?跑Andersson

_____
http://www.guffa.com


确定。想想我必须遗漏一些东西。会尝试一下。在

的注释中尝试... catch块,如何捕获所有异常并在

messageBox中显示它们(出于调试原因。当我运行下面的代码时,我从来没有得到过一个

异常,除非系统只是把它扔掉而且我好了b $ b不知道它。

" ; G?ran Andersson"< gu *** @ guffa.comwrote in message

news:eG ************** @ TK2MSFTNGP05.phx.gbl。 ..


Andy B写道:


>我在WebBrowser中有以下代码。 DocumentCompleted事件:

对于索引As Integer = 0 To
Me.Browser.Document.GetElementsByTagName(" ul")。it em(0).GetElementsByTagName(" li")。计数

NotesList.Items.Add(Me.Browser.Document.GetElemen tsByTagName(" ul")。Item(0).GetElementsByTagName(" li")。Item(index).InnerText )

下一个索引

由于某种原因,之后什么都没有下一个索引将运行。它被忽略之后就像
一切。即使我刚刚使用它,它也不会起作用(尽管如果我这样做,我会得到错误的数据)。你是如何解决这个问题的?



我想你在某个地方捕捉异常?


在循环的最后一次迭代中,你试图访问一个

在列表之外的项目,所以你得到一个IndexOutOfRangeException,它将把b / b
的代码执行发送到catch块中,跳过循环后代码




你永远不应该只是抓住一个异常并默默地扔掉它

(除非是非常小的尝试... catch块你指定的确是

你期望什么例外,并且确切地知道为什么你可以忽略它。)


索引Next语句中的规范是可选的,并且不包括

它不会以任何方式改变循环的行为。


你应该从0循环到Count-1,而不是从0到Count:


将项目设为HtmlElementCollection =

Me.Browser.Document.GetElementsByTagName(" ul")。我是(0).GetElementsByTagName(" li")

索引= 0到items.Count - 1

NoteList.Items.Add(items.Item(index)。 InnerText)

下一页


-

G?跑Andersson

_____
http://www.guffa.com



I have the following code inside of a WebBrowser.DocumentCompleted event:

For index As Integer = 0 To
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName("li").Count

NotesList.Items.Add(Me.Browser.Document.GetElement sByTagName("ul").Item(0).GetElementsByTagName("li" ).Item(index).InnerText)

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?


解决方案

In this kind of loop it is normal

Count - 1

Cor

"Andy B" <a_*****@sbcglobal.netwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...

>I have the following code inside of a WebBrowser.DocumentCompleted event:

For index As Integer = 0 To
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName("li").Count

NotesList.Items.Add(Me.Browser.Document.GetElement sByTagName("ul").Item(0).GetElementsByTagName("li" ).Item(index).InnerText)

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?


Andy B wrote:

I have the following code inside of a WebBrowser.DocumentCompleted event:

For index As Integer = 0 To
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName("li").Count

NotesList.Items.Add(Me.Browser.Document.GetElement sByTagName("ul").Item(0).GetElementsByTagName("li" ).Item(index).InnerText)

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 IndexOutOfRangeException, 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 HtmlElementCollection =
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName("li")
For index = 0 to items.Count - 1
NoteList.Items.Add(items.Item(index).InnerText)
Next

--
G?ran Andersson
_____
http://www.guffa.com


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.comwrote in message
news:eG**************@TK2MSFTNGP05.phx.gbl...

Andy B wrote:

>I have the following code inside of a WebBrowser.DocumentCompleted event:

For index As Integer = 0 To
Me.Browser.Document.GetElementsByTagName("ul").It em(0).GetElementsByTagName("li").Count

NotesList.Items.Add(Me.Browser.Document.GetElemen tsByTagName("ul").Item(0).GetElementsByTagName("li ").Item(index).InnerText)

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 IndexOutOfRangeException, 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 HtmlElementCollection =
Me.Browser.Document.GetElementsByTagName("ul").Ite m(0).GetElementsByTagName("li")
For index = 0 to items.Count - 1
NoteList.Items.Add(items.Item(index).InnerText)
Next

--
G?ran Andersson
_____
http://www.guffa.com



这篇关于for循环被打破了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆