I try to make/create a persistent variable.
This is the code I use for testing, but setting the persist property does not seem to have any effect.
The script always exists with my little debug output: "Var is still not persistent, weird?"
function OnClick(data){
var vars = DOpus.vars;
var varName = "MyResult";
if (!vars.Exists(varName)){
DOpus.Output("Var not exists yet, creating it!");
vars.Set(varName,"value");
} else {
DOpus.Output("Var exists.");
}
var theVar = vars.Get(varName);
if (theVar.persist){
DOpus.Output("Var has been made persistent before, ok.");
return;
} else {
DOpus.Output("Var is not persistent yet, making it persistent..");
theVar.persist = true;
}
if (theVar.persist)
DOpus.Output("Var is persistent now, all is fine!");
else{
DOpus.Output("Var is *still not* persistent, weird?");
}
}
I think your "theVar" is a string, not an object and .persists operates on a VAR object. The following VBScript code, modelled on your JScript code, seems to work.
Option Explicit
Function OnClick(data)
Dim vars, varName, theVar, objVar
Set vars = DOpus.vars
varName = "MyResult"
If Not (vars.Exists(varName)) Then
DOpus.Output "Var not exists yet, creating it!"
vars.Set varName,"value"
Else
DOpus.Output "Var exists."
End If
theVar = vars.Get(varName)
DOpus.Output "theVar = " & theVar
Set objVar = vars(theVar)
If (objVar.persist) Then
DOpus.Output "Var has been made persistent before, ok."
Exit Function
Else
DOpus.Output "Var is not persistent yet, making it persistent.."
objVar.persist = True
End If
If (objVar.persist) Then
DOpus.Output "Var is persistent now, all is fine!"
Else
DOpus.Output "Var is *still not* persistent, weird?"
End If
End Function
Set objVar = vars(varName) ' This correctly sets objVar to "MyResult"
replaces
Set objVar = vars(theVar) ' This incorrectly sets objVar to the _value_ of "MyResult"
@language vbscript
Option Explicit
Function OnClick(data)
Dim vars, varName, theVar, objVar
Set vars = DOpus.vars
varName = "MyResult"
If Not (vars.Exists(varName)) Then
DOpus.Output "Var not exists yet, creating it!"
vars.Set varName,"value"
Else
DOpus.Output "Var exists."
End If
theVar = vars.Get(varName)
DOpus.Output "theVar = " & theVar
Set objVar = vars(varName)
If (objVar.persist) Then
DOpus.Output "Var has been made persistent before, ok."
Exit Function
Else
DOpus.Output "Var is not persistent yet, making it persistent.."
objVar.persist = True
End If
If (objVar.persist) Then
DOpus.Output "Var is persistent now, all is fine!"
Else
DOpus.Output "Var is *still not* persistent, weird?"
End If
End Function
Your vbscript version works as expected. Thanks for that! o) I understand what mistake I made, I just got the value not the var object itself.
So I need to have an js-equivalent to this vbscript line:
Set objVar = vars(varName)
I think this should work:
theVar = vars[varName];
but theVar is undefined, while vars.Exists(varName) returns true.
Confused I am.. o)
ps: The description on the [vars collection].Get() method says: "You can use this method as an alternative to indexing the collection."
To me, "indexing" is what's shown above in the last code block - direct access to an element inside an array/collection, is this wrong?
Thank you Leo, that works and get's me going with those persistent variables in jscript! o)
--
I'm still a bit irritated and curious to know -> What kind of thing is DOpus.Vars?
It's not an associative js-array, as indexing by "[]" does not work.
It's no jscript method, because echoing DOpus.Vars results in nothing ("") being output.
It's no js-object, because echoing those objects normally results in a string "[object Object]" e.g.
It's no activex object, because echoing DOpus.Vars (omitting method braces) does output "", instead of "Object does not support this property or method".
It's no method on an activex object, because those cannot be assigned to js-variables.
A line of code like objVar = vars(varName); is very unusual if vars is not a simple js-function, which it's not, as it has accessible (sub)-methods.
It's like vars is a method and object containing methods at the same time, reminds me of the magic "invoke()" in php.
I'm doing jscript for about 14 years and this is something I did not come across yet, plz let me know! o)
I'm not sure exactly what it is (not my department). I expect it's an IDispatch ActiveX object that implements certain methods and properties.
ActiveScripting is frustrating because is seems to have about 150 different ways of representing collections, some varying by language, all inconsistent with each other, and most of them fairly terrible and inflexible (so you can't just use one and ignore the others; you have to use all the different ones in different situations).
I already noticed from various posts of you, I think especially jon, that it wasn't necessarily much fun implementing the scripting into DO, but you made it! o)
Maybe jon drops in and shares some more details on this vars-thing, if not, I'm going to put this object/method/collection or what kind of hybrid it is into the "magic things of opus" drawer.