Ask a Question

Increment a variable by 1?

alexpat
Occasional Contributor

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

do

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 5
tppegu
Contributor

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
Next
alexpat
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.
YMinaev
Staff

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.
------
Yuri
TestComplete Customer Care Engineer

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
alexpat
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
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



Regards

Stephen.
cancel
Showing results for 
Search instead for 
Did you mean: