Search code examples
c#jscriptclearscript

How to update a Dictionary entry in ClearScript?


Context: ClearScript, JScript, C#, Windows, Azure

In my ClearScript-enabled projects, I have a Dictionary<string,object> that I use for passing data into, around inside and back out of evaluated scripts.

On the C# side I have

static Dictionary<string, object> Settings = new Dictionary<string, object>();

and then later on

JSengine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.EnableJITDebugging);

and

JSengine.AddHostObject("CSSettings", Settings);

On the JScript side I have things like

CSSettings.Add("your API key", CSConfig.Retrieve("api.key"));

for setting values.

The challenge at the moment is updating a value in the Dictionary. The following works

CSSettings.Item("id") = Wfm_AccNumber;

it's just that it's non-standard JScript. What's more the JSHint tool that I'm using inside of Notepad++ complains.

I could do a .Remove() before the .Add(), I suppose but is there a better way?


Solution

  • The following should work:

    CSSettings.Item.set("id", Wfm_AccNumber);
    value = CSSettings.Item.get("id"); // or CSSettings.Item("id")
    

    This may look a bit clunky, but it's standard JavaScript and should work with all .NET indexers (which aren't always named "Item", and can have more than one parameter).