Reading Dictionary entries and other indexed properties in JavaScript

Aug 2, 2013 at 9:53 PM
I'm sure I'm missing something very basic, but here goes: If I pass a Dictionary from my C# application to a JavaScript function as a function parameter, existing dictionary member items are inaccessible to the JavaScript (the error is something along the lines of "Object has no property or field named 'XXXXX'"), and yet I can add new dictionary items from JavaScript that are visible to the calling C# process. For example:

C# code:
Dictionary<string,string> fieldValues = new Dictionary<string,string>();
fieldValues.Add("FIRST_NAME", "John");
fieldValues.Add("LAST_NAME", "Doe");

// Invoke the Javascript code shown below:
dynamic result = engine.Script.validateFields(fieldValues); // Result == 3

// I can see the new item added in the script:
string address = fieldValues["ADDRESS"]; // 123 Anywhere Street
JavaScript code:
function validateFields(fieldValues)
{
    // If uncommented, the next line always fails:
    fieldValues["FIRST_NAME"] = "Johnny";

    // But I can add new items...
    fieldValues.Add("ADDRESS", "123 Anywhere Street");

    // And this returns 3...
    return fieldValues.Count;
}
This problem seems to also occur with instances of any class that has indexed properties, not just the Dictionary class. For example, I can't seem to get instances of System.Data.DataRow objects to work at all in JavaScript. Every time I try to access the indexed data value property, I get an error (e.g., dataRow["ZIP_CODE"] results in an error that there is no such field or property named "ZIP_CODE").

So... is this a bug with all indexed properties in general, or is it the expected behavior? Any workaround suggestions? Thanks...
Coordinator
Aug 2, 2013 at 10:40 PM
Edited Aug 3, 2013 at 3:39 AM
Greetings sgammans, and thanks for your question!

Beneath the cover of C# syntax, an indexer is actually an indexed property named Item. .NET has generic cross-language support for indexed properties, but C# only uses this feature to implement indexers.

Anyway, ClearScript supports the following script syntax for accessing your indexed property:
// JavaScript
fieldValues.Item.get("FIRST_NAME");
fieldValues.Item.set("FIRST_NAME", "Johnny");
Cheers!
Aug 5, 2013 at 3:16 PM
...aaaaaand since the Item syntax is masked by indexers in C# I forgot it was there, so it didn't occur to me to try that in JavaScript. D'OH! Thank you very much for the fast reply. Actually, I admit that I am flabbergasted by the speediness of your reply--I posted my query at 4:53 PM on Friday, not expecting to get a reply until sometime today. Instead, you replied less than an hour later! Reading through some of the other discussions, though, I see that prompt replies are more the norm than the exception, which gives me a measure of confidence about the ClearScript community.

Just as a quick bit of background, we currently are using the JScript.Vsa.VsaEngine to interpret JavaScript in our C# application. The JavaScript consists of end-user-defined formulas that extend our base system, so it's a core feature we need to protect, and VsaEngine's been marked obsolete for a couple versions of Visual Studio! I've been researching replacement candidates for a few weeks now, and so far ClearScript is shaping up to be the front-runner.

Again, thanks!
Coordinator
Aug 5, 2013 at 9:17 PM
Thanks for the positive feedback! Please don't hesitate to let us know about any further issues you encounter.