Parent-child script relation

Mar 10, 2014 at 8:37 AM
Hi,

Following code does not work. Expected result is 20.
I have one global script and one child script that use it.
numValue variable from the global script is not available from in child script.
using(var globalScript = new VBScriptEngine())
using(var childScript = new VBScriptEngine())
{
    Console.WriteLine("Start VBScriptEngine");
    globalScript.Script.numValue = 4;
    childScript.Script.GlobalScript = globalScript.Script;
    Console.WriteLine(childScript.Evaluate("5 * numValue"));
    Console.WriteLine("End script");
    Console.ReadKey();
}
Coordinator
Mar 10, 2014 at 1:03 PM
Edited Mar 10, 2014 at 1:08 PM
Hello ifle,

Since you're exposing the parent's global object in the child under the name "GlobalScript", you probably meant to write this:
Console.WriteLine(childScript.Evaluate("5 * GlobalScript.numValue"));
By the way, we don't recommend exposing script items to other script engines. It may work, but it isn't well tested and there are scenarios that are known to be problematic, especially when different script engine classes are involved.

Cheers!
Mar 10, 2014 at 2:07 PM
Edited Mar 10, 2014 at 2:08 PM
This way that works our legacy application.
My example is very simple, but our global script contains hundreds functions that can be called from a customer child script functions
Mar 11, 2014 at 12:26 PM
Edited Mar 11, 2014 at 12:26 PM
Sorry. This my mistake, our application used with AddNamedItem with globalvariable flag.
Coordinator
Mar 11, 2014 at 1:05 PM
Hi ifle,

This seems to work:
using (var globalScript = new VBScriptEngine())
using (var childScript = new VBScriptEngine())
{
    Console.WriteLine("Start VBScriptEngine");
    globalScript.Script.numValue = 4;
    childScript.AddHostObject("GlobalScript", HostItemFlags.GlobalMembers, globalScript.Script);
    Console.WriteLine(childScript.Evaluate("5 * numValue"));
    Console.WriteLine("End script");
    Console.ReadKey();
} 
However, again, it isn't recommended. AddHostObject() is intended to be used with, well, host objects :)

Instead of exposing script items across engines, would it not be possible to load everything you need into the child engine?
Mar 12, 2014 at 2:35 PM
Edited Mar 14, 2014 at 2:32 PM
Instead of exposing script items across engines, would it not be possible to load everything you need into the child
engine?
This is legacy code. We can't change it.

I understand, thanks
Mar 13, 2014 at 7:18 PM
Edited Mar 13, 2014 at 7:18 PM
Small question about setting global variables.
Suppose I have following script that has account variable.
dim account

function GetAccountName()
GetAccountName = account.Name;
end function
What is "true" way set this variable by reference?

Thanks
Coordinator
Mar 14, 2014 at 2:54 AM
Greetings!

What is "true" way set this variable by reference?

Sorry, please clarify. Are you asking how to set the account variable from the host?
Mar 14, 2014 at 7:05 AM
Edited Mar 14, 2014 at 3:20 PM
I rechecked, that works as expected. Thanks