GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Declaing Global Variable in Opus VB Script


#1

I would like to assign a value to a global variable in the Function OnInit(initData) (which would basically be a constant in the whole script) so it would be accessible to all the functions in a script. Is it possible at all doing it?
I have tried declaring it using the "Public" keyword but the scripts didn't accept it and throws an error.


#2

To make a global variable in VBScript you just define the variable outside of any functions, usually at the top of the script.

But also note that Opus may start and stop your script multiple times, depending on the type of script. (For example, event-driven scripts.)

If you want a variable that outlives your script, so that it can be modified by one instance of the script and later read back by another instance, Opus provides various Vars collections which can be used for that, tying the lifetime of a variable to a script, lister, or tab, and optionally saving it to disk so it can survive reboots, if desired.


#3

I declared a variable in the Function OnInit(initData)
Dim MyVar
MyVar = "TheValOfMyVar"
Then I declared a function
Function GetVal()
dopus.output MyVar
End Function

But the script throws an Error Variable is undefined: 'myvar' (0x800a01f4)
I just need to declare constants in the beginning of the script so that all the function (functions that are considered as NEW commands
Set cmd1 = initData.AddCommand
cmd1.name = "GetVal"
)


#4

That is not "outside of any functions", is it? :slight_smile:


#5

Thank you it is fine now.