Forum Discussion

mgroen's avatar
mgroen
Frequent Contributor
11 years ago

Increasing variable value

Hi,

I have defined a local variable named tellertje of type integer;



In keyword testing I use Code Snippet, and use following code:

Keywordtests.test1.Variables.tellertje = Keywordtests.test1.Variables.tellertje + 1



However when running the test the value remains 0 (it's initial value)



What am I doing wrong?

  • Mathijs,

    I believe that Andrew's solution should work. I attached an image of what the keyword dialog should look like using his suggestion.

    Let us know if this works for you.

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Mathijs,



    In VBScript, x = y can mean two things:


    • An assignment, where the value of y is assigned to x.


    • A comparison of the x and y values. If the values are the same, the result is True; if they are not, the result is False.



     



    In VBScript projects, Run Code Snippet works like VBScript's Eval function and treats = as a comparison. To treat = as an assignment, you need to wrap the code into the Execute statement:



    Or you can use Set Variable Value with this Value:


  • bukgameos's avatar
    bukgameos
    New Contributor
    Try to use "Statements -> Set variable value" and there increase your variable as "Code expression"
  • mgroen's avatar
    mgroen
    Frequent Contributor
    Daniel,



    thank you for the additional information.

    That actually did the trick.



    I made an error in the statement in the Code Expression box:

    instead of keywordtests.variables.tellertje + 1



    I used:

    keywordtests.variables.tellertje = keywordtests.variables.tellertje + 1



    Thanks both for the info!!
  • mgroen's avatar
    mgroen
    Frequent Contributor
    Andrew,



    I have tried that, but does not work. The variable remains keeping its initial value (0), and does not get increased.



    See attached screenshot. I expected that the while loop would run 5 times. However the loop keeps running endless.



    Also, I noticed that if I make a separate script function for increasing the variable, and invoke it the "Run script routine" the variable is increased.



    Is this a malfunction in TC?



  • Mathijs,

    I believe that Andrew's solution should work. I attached an image of what the keyword dialog should look like using his suggestion.

    Let us know if this works for you.

  • Mathijs,

    Glad to hear you got it working!



    Helen,

    Thanks for the great detail about how the Run Code Snippet works!

    Perhaps this might be useful detail to include in the TestComplete documentation?  I know it is specific to VBScript, but VBS is pretty widely used so I would guess that this info will help many TestComplete users.



    Happy Thursday!
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Daniel,



    Thanks for the feedback! I've added this to our TODO list.