Forum Discussion

alexpat's avatar
Occasional Contributor
14 years ago

Increment a variable by 1?

I have a loop I only want to run a maximum of 10 times. How do I get a variable to increase everytime the loop runs?

if expectFailure then

dim i

i = 0


aqUtils.Delay 100

set obj = browser.NativeWebObject.Find("class", "confirmboxcont")

set i = i + 1

loop until obj.Exists or i >= 10

end if

As far as I can see this should work but I obviously have some syntax incorrect somewhere.

5 Replies

  • I don't use VB at all, so these things may not be issues, but I notice two things that look wrong to me. First, you use set to increment the counter, but not for the initial assignment to 0. Second, you do not dim the obj variable.

    As a matter of style though, "For" loops are usually preferred in cases where you need a counter. Opinions certainly differ, but something like this would generally be considered better:

    For i = 1 to 10
    aqUtils.Delay 100 
    set obj = browser.NativeWebObject.Find("class", "confirmboxcont")
    If obj.Exists Then
    Exit For
  • alexpat's avatar
    Occasional Contributor
    The problem is I don't want it to run 10 times. I want it to run ten times at the most before exiting so I don't get stuck in a loop if the do loop exit parameter doesn't become available.
  • Hi,

    Actually, Tony's code does exactly what you need. It tries to obtain an object ten times and exits the loop right when the object is found even if ten iterations haven't passed.
  • alexpat's avatar
    Occasional Contributor
    Ah, so it does. Guess I should have looked at the code harder.

    I'm really not very good at this apparently. :-)
  • sbkeenan's avatar
    Frequent Contributor
    Hi Alex

    Something else you might find useful for future reference is that when assigning a value to a variable, you do not need the Set statement.  Use Set only when assigning an object to a variable.

    So your line, Set i = i + 1, should simply read i = i + 1