Home All Groups Group Topic Archive Search About
Author
13 Apr 2005 3:15 AM
Microsoft
I have the following loop the length contains somewhere around 1000+ items:

For elemIndex = 0 To len - 1
elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
     If elem.tagName = "A" Then
          If elem.innerText = "1." Then
          elem.click()
     End If
End If
Next elemIndex

Once I find the item  I am looking for I want to end the loop. As it is now
even though I have issues the elem.click event it processes all 1000+ items.
Once I find what I am looking for and act on it, I want the loop to be done.
Just not sure how to accomplish that.

S

Author
13 Apr 2005 3:39 AM
Scott M.
When you know you don't want to continue looping add "Exit For".

By the way, is "len" the name of one of your variables?  "Len" is a VB 6.0
function that still is accessible in VB.NET and so you should probably not
use it as a variable name.


Show quoteHide quote
"Microsoft )IntraRELY.com>" <ng(<at> wrote in message
news:unCqjb9PFHA.2560@TK2MSFTNGP14.phx.gbl...
>I have the following loop the length contains somewhere around 1000+ items:
>
> For elemIndex = 0 To len - 1
> elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
>     If elem.tagName = "A" Then
>          If elem.innerText = "1." Then
>          elem.click()
>     End If
> End If
> Next elemIndex
>
> Once I find the item  I am looking for I want to end the loop. As it is
> now even though I have issues the elem.click event it processes all 1000+
> items. Once I find what I am looking for and act on it, I want the loop to
> be done. Just not sure how to accomplish that.
>
> S
>
Author
13 Apr 2005 4:21 AM
Ray Cassick (Home)
Personaly (I know you didn't ask but I love to nose in :) ),

I don't like to exit for loops aheadof time. For loops are for (no pun
intended) when you want to loop a known number of times, period.

I would work on a way using a while loop, flag and a counter...

Warning.. not tested... off the top of my head code...

        Do
            If (elemCounter < (len - 1)) Then
                elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
                If elem.tagName = "A" Then
                    If elem.innerText = "1." Then
                        elem.click()
                        foundIt = True

                    End If

                End If
                elemIndex += 1

            End If
        Loop While (foundIt = False)


Show quoteHide quote
"Scott M." <s-mar@nospam.nospam> wrote in message
news:ewicxq9PFHA.1396@TK2MSFTNGP10.phx.gbl...
> When you know you don't want to continue looping add "Exit For".
>
> By the way, is "len" the name of one of your variables?  "Len" is a VB 6.0
> function that still is accessible in VB.NET and so you should probably not
> use it as a variable name.
>
>
> "Microsoft )IntraRELY.com>" <ng(<at> wrote in message
> news:unCqjb9PFHA.2560@TK2MSFTNGP14.phx.gbl...
>>I have the following loop the length contains somewhere around 1000+
>>items:
>>
>> For elemIndex = 0 To len - 1
>> elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
>>     If elem.tagName = "A" Then
>>          If elem.innerText = "1." Then
>>          elem.click()
>>     End If
>> End If
>> Next elemIndex
>>
>> Once I find the item  I am looking for I want to end the loop. As it is
>> now even though I have issues the elem.click event it processes all 1000+
>> items. Once I find what I am looking for and act on it, I want the loop
>> to be done. Just not sure how to accomplish that.
>>
>> S
>>
>
>
Author
13 Apr 2005 6:01 AM
Cor Ligthert
Ray,

I don't agree with you completly. However when it becomes nested than you
are right in my opinion.

Although than I use the "Do Until" which describes better direct at the
start what you are doing without first searching for the end of the routine.

Just my thought,

Cor
Author
13 Apr 2005 12:31 PM
Chris
I disagree with you on this for several reasons.

1. You way takes more lines of code, therefore more places to make mistakes.

2. For loops are to count a number of known iterations, which is what he
has.  The for loop has a set exit time. If it didn't, then it would just be
like a while loop.  You will notice in your untested code, you would loop
endlessly if the item is never found.  Yes, you could add another check to
make sure it doesn't, but there again, your making the code more complex for
no benefit.

3. You give no reason why it is bad to exit the loop ahead of time.

Chris



Show quoteHide quote
"Ray Cassick (Home)" <rcassickNOSPAM@enterprocity.com> wrote in message
news:u6Hl4A%23PFHA.1396@TK2MSFTNGP10.phx.gbl...
> Personaly (I know you didn't ask but I love to nose in :) ),
>
> I don't like to exit for loops aheadof time. For loops are for (no pun
> intended) when you want to loop a known number of times, period.
>
> I would work on a way using a while loop, flag and a counter...
>
> Warning.. not tested... off the top of my head code...
>
>        Do
>            If (elemCounter < (len - 1)) Then
>                elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
>                If elem.tagName = "A" Then
>                    If elem.innerText = "1." Then
>                        elem.click()
>                        foundIt = True
>
>                    End If
>
>                End If
>                elemIndex += 1
>
>            End If
>        Loop While (foundIt = False)
>
>
> "Scott M." <s-mar@nospam.nospam> wrote in message
> news:ewicxq9PFHA.1396@TK2MSFTNGP10.phx.gbl...
>> When you know you don't want to continue looping add "Exit For".
>>
>> By the way, is "len" the name of one of your variables?  "Len" is a VB
>> 6.0 function that still is accessible in VB.NET and so you should
>> probably not use it as a variable name.
>>
>>
>> "Microsoft )IntraRELY.com>" <ng(<at> wrote in message
>> news:unCqjb9PFHA.2560@TK2MSFTNGP14.phx.gbl...
>>>I have the following loop the length contains somewhere around 1000+
>>>items:
>>>
>>> For elemIndex = 0 To len - 1
>>> elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
>>>     If elem.tagName = "A" Then
>>>          If elem.innerText = "1." Then
>>>          elem.click()
>>>     End If
>>> End If
>>> Next elemIndex
>>>
>>> Once I find the item  I am looking for I want to end the loop. As it is
>>> now even though I have issues the elem.click event it processes all
>>> 1000+ items. Once I find what I am looking for and act on it, I want the
>>> loop to be done. Just not sure how to accomplish that.
>>>
>>> S
>>>
>>
>>
>
>
Author
14 Apr 2005 12:43 AM
Ray Cassick (Home)
"Chris" <NoSpam@NoSpam.com> wrote in message
news:OpkS%23SCQFHA.2580@TK2MSFTNGP10.phx.gbl...
>I disagree with you on this for several reasons.
>
> 1. You way takes more lines of code, therefore more places to make
> mistakes.
>

We are all entitled to our opinions, but I am from the school that says,
even if it is more lines, if the intent is clearer the that means it is
easier to maintain and fix in the future.

> 2. For loops are to count a number of known iterations, which is what he
> has.  The for loop has a set exit time. If it didn't, then it would just
> be like a while loop.  You will notice in your untested code, you would
> loop endlessly if the item is never found.  Yes, you could add another
> check to make sure it doesn't, but there again, your making the code more
> complex for no benefit.
>

This was the exact reason for my warning... "Warning.. not tested... off the
top of my head code..." It was off the cuff, done in the news editor, not
the code editor, not compiled or tested, and it was 12:21am...

Yes, you have a defined number of things to loop through, but the intent of
the code is not to always run that number of times. The intent is to look
through as many as needed and stop looking when the one you look for is
found. The for loop is analogous to looking through a deck of 52 cards
looking for the ace of spades and even if you find it on the third card you
keep looking. Yes, you can exit the for loop early, but to me that induces
the chance that if the code needs to be changed in the future errors might
creep in.

I am also a firm believer in the idiom of single exit points for functions.
I hate people that do things like this:

Public Function Foo(thingToWatch) as Boolean

    'Looks for thing one and returns True when found
    Select Case thingToWatch
        Case ThingOne
            return True

        Case ThingTwo
            return False

        Case Default
            return False

    End Select

End Function

To me, exiting a for loop is the same nasty looking thing.


> 3. You give no reason why it is bad to exit the loop ahead of time.
>

Boy I tell you the web has let me down on this one. I have had several
documents over the years that have documented 'proper' styles for looping
structures and I have adopted them. This was even a very nitpicky area for
my teachers in college. Looking at the possible exit points in the code,
execution starts at the top and flows downwards. Decision statements can
branch out of a structure, loops simply repeat things. Decisions structures
branch, loops iterate and let the code flow.

Be damned if I can find a reference that is 'webable' right now...

Again, I guess it is more of a matter of taste and style.

Show quoteHide quote
> Chris
>
>
>
> "Ray Cassick (Home)" <rcassickNOSPAM@enterprocity.com> wrote in message
> news:u6Hl4A%23PFHA.1396@TK2MSFTNGP10.phx.gbl...
>> Personaly (I know you didn't ask but I love to nose in :) ),
>>
>> I don't like to exit for loops aheadof time. For loops are for (no pun
>> intended) when you want to loop a known number of times, period.
>>
>> I would work on a way using a while loop, flag and a counter...
>>
>> Warning.. not tested... off the top of my head code...
>>
>>        Do
>>            If (elemCounter < (len - 1)) Then
>>                elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
>>                If elem.tagName = "A" Then
>>                    If elem.innerText = "1." Then
>>                        elem.click()
>>                        foundIt = True
>>
>>                    End If
>>
>>                End If
>>                elemIndex += 1
>>
>>            End If
>>        Loop While (foundIt = False)
>>
>>
>> "Scott M." <s-mar@nospam.nospam> wrote in message
>> news:ewicxq9PFHA.1396@TK2MSFTNGP10.phx.gbl...
>>> When you know you don't want to continue looping add "Exit For".
>>>
>>> By the way, is "len" the name of one of your variables?  "Len" is a VB
>>> 6.0 function that still is accessible in VB.NET and so you should
>>> probably not use it as a variable name.
>>>
>>>
>>> "Microsoft )IntraRELY.com>" <ng(<at> wrote in message
>>> news:unCqjb9PFHA.2560@TK2MSFTNGP14.phx.gbl...
>>>>I have the following loop the length contains somewhere around 1000+
>>>>items:
>>>>
>>>> For elemIndex = 0 To len - 1
>>>> elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
>>>>     If elem.tagName = "A" Then
>>>>          If elem.innerText = "1." Then
>>>>          elem.click()
>>>>     End If
>>>> End If
>>>> Next elemIndex
>>>>
>>>> Once I find the item  I am looking for I want to end the loop. As it is
>>>> now even though I have issues the elem.click event it processes all
>>>> 1000+ items. Once I find what I am looking for and act on it, I want
>>>> the loop to be done. Just not sure how to accomplish that.
>>>>
>>>> S
>>>>
>>>
>>>
>>
>>
>
>
Author
13 Apr 2005 5:12 PM
Roger
<<
For loops are for (no pun
intended) when you want to loop a known number of times, period.
>>

A known number of times, or a known number of
elements?

Roger

<<
For elemIndex = 0 To len - 1
     elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
Show quoteHide quote
>>
Author
13 Apr 2005 8:34 PM
Microsoft
Thanks,

I agree that using the do while would best suite my situation.

Steve

Show quoteHide quote
"Ray Cassick (Home)" <rcassickNOSPAM@enterprocity.com> wrote in message
news:u6Hl4A%23PFHA.1396@TK2MSFTNGP10.phx.gbl...
> Personaly (I know you didn't ask but I love to nose in :) ),
>
> I don't like to exit for loops aheadof time. For loops are for (no pun
> intended) when you want to loop a known number of times, period.
>
> I would work on a way using a while loop, flag and a counter...
>
> Warning.. not tested... off the top of my head code...
>
>        Do
>            If (elemCounter < (len - 1)) Then
>                elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
>                If elem.tagName = "A" Then
>                    If elem.innerText = "1." Then
>                        elem.click()
>                        foundIt = True
>
>                    End If
>
>                End If
>                elemIndex += 1
>
>            End If
>        Loop While (foundIt = False)
>
>
> "Scott M." <s-mar@nospam.nospam> wrote in message
> news:ewicxq9PFHA.1396@TK2MSFTNGP10.phx.gbl...
>> When you know you don't want to continue looping add "Exit For".
>>
>> By the way, is "len" the name of one of your variables?  "Len" is a VB
>> 6.0 function that still is accessible in VB.NET and so you should
>> probably not use it as a variable name.
>>
>>
>> "Microsoft )IntraRELY.com>" <ng(<at> wrote in message
>> news:unCqjb9PFHA.2560@TK2MSFTNGP14.phx.gbl...
>>>I have the following loop the length contains somewhere around 1000+
>>>items:
>>>
>>> For elemIndex = 0 To len - 1
>>> elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
>>>     If elem.tagName = "A" Then
>>>          If elem.innerText = "1." Then
>>>          elem.click()
>>>     End If
>>> End If
>>> Next elemIndex
>>>
>>> Once I find the item  I am looking for I want to end the loop. As it is
>>> now even though I have issues the elem.click event it processes all
>>> 1000+ items. Once I find what I am looking for and act on it, I want the
>>> loop to be done. Just not sure how to accomplish that.
>>>
>>> S
>>>
>>
>>
>
>
Author
13 Apr 2005 3:40 AM
Chris
For elemIndex = 0 To len - 1
     elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
     If elem.tagName = "A" Then
          If elem.innerText = "1." Then
              elem.click()
              Exit For
          End If
    End If
Next elemIndex



Show quoteHide quote
"Microsoft )IntraRELY.com>" <ng(<at> wrote in message
news:unCqjb9PFHA.2560@TK2MSFTNGP14.phx.gbl...
>I have the following loop the length contains somewhere around 1000+ items:
>
> For elemIndex = 0 To len - 1
> elem = CType(doc.all.item(elemIndex), mshtml.IHTMLElement)
>     If elem.tagName = "A" Then
>          If elem.innerText = "1." Then
>          elem.click()
>     End If
> End If
> Next elemIndex
>
> Once I find the item  I am looking for I want to end the loop. As it is
> now even though I have issues the elem.click event it processes all 1000+
> items. Once I find what I am looking for and act on it, I want the loop to
> be done. Just not sure how to accomplish that.
>
> S
>