I thought I knew all about variable scoping and life time in QTP/VBScript and then I encountered behavior that I do not understand.
I have a .vbs file that is associated to the script. In it I tried declaring a variable as Dim mVar. The purpose is so that it will be available everyplace. It should be seen in all actions, inside and outside of functions in all .vbs files. In case Dim was not correct I also tried using Public.
In an action, the script uses the variable that was just declared in the .vbs file.
I call it like this:
Note: Maybe the Call is not needed.
The function is inside of the same .vbs where the variable was declared. I tried declaring the variable with and without ByRef and neither fixed my issue.
Inside the function, I set the value of the mVar variable and then leave the function.
After calling the function, the value of the parameter is shown as unknown in the debugger.
OOOH. I just realized that the variable is Public so I shouldn't need to be passing it into the funciton.
Let me see what happens if I don't pass the variable and change its value.
I may be answering part of my question by typing out loud.
When in Florida, Don't Tampa with the code. I made this up.
How are you loading the .vbs file? I think it could make a difference. If you're attaching it to the test through the Resources, then a public variable in the function library should be visible (and editable) everywhere.
If you're using ExecuteFile to load the .vbs file then the variable might be limited to the scope of the current action... but i'm not sure. I never use ExecuteFile.
What you are observing is correct. When you use a variable declared in a ***ociated library file, the variable can be used as a normal variable. But when you use the same in an Action, because the scope changes it cannot be p***ed by reference. Consider the below code
and library code as
Test = "Tarun"
Function Best(ByRef val)
Val = "Lalwani"
Both the message boxes will give output as "Tarun". When a global variable is used in an Action, QTP internally patches the same to the global library using VBScript internal features. But it is not a variable present in your scope, the same is the case of the cl***es. That is why we cannot use New Cl***Name in actions for a cl*** defined in ***ociated library file
Dennis...I'm going through resources. My .vbs files are inside of QTP.
Tarun...This is what I am seeing. I have seen code where the cl***es are described in .vbs files. I can communicate with the methods and properties from a calling action. How is this working?
I purchased your new book a couple of days ago and it should arrive any time. I saw other people say that your books should cost less. I do NOT agree. Please charge more. You are providing a service that saves me loads of frustration and time for both me and my clients.
Kartikpai... I didn't know how to use private messaging with the old Web Interface on this site. I will try to figure out where the Private messaging is working.
I hope it is OK that I responded to everyone in one post. Is there a better way?