June 12, 2008

Option Explicit and other QTP curiosities

I like variable declaration and strong type casting. It makes sense, because it makes sense in assembly. And it prevents me from making mistakes in variable names. QTP offers variable declaration with the command "option explicit". This works just the same as in VBA or VB, but... you need to be consequent. Every action script, function library or external vbs file needs an option explicit command or else it won't work. You thought you didn't need it in that tiny class file where mistakes are not possible? You thought wrong. Ignoring one option explicit, makes all other option explicits obsolete and your scripts will be interpreted without the need for variable declaration.

By the way, undeclared variable detection only works during runtime. There is no pre-compile option as in VB or VBA, so it is good old trial and error when you want to debug your script on undeclared variables.

"Randomize" has sort of the same behavior: Don't put it in a header script or such kind, but put it in the library where you use the rnd function. If you use rnd in multiple libraries, you'll have to put Randomize in each of them, otherwise you are stuck with the same 'random' numbers each time you run the script.

No comments: