This project has moved and is read-only. For the latest updates, please go here.

dictionary returns undefined

Jan 1, 2015 at 4:19 PM
dictionary returns undefined but the item exist
public class Get : Dictionary<string, string>
...
var b = _GET["blub"];
thanks for help
Coordinator
Jan 2, 2015 at 2:20 PM
Hi furesoft,

For standard .NET dictionaries, the following should work:
var b = _GET.Item.get('blub'); // _GET.Item('blub') also works but is not preferred
_GET.Item.set('blub', foo);
Another possibility is to use the provided PropertyBag class instead of a standard dictionary. That'll enable simplified JavaScript-like syntax for looking up and storing values, but other members (e.g., methods such as TryGetValue) will not be accessible from script code.

Good luck!
Jan 2, 2015 at 3:53 PM
Edited Jan 2, 2015 at 3:55 PM
but another problem is, the get class is in another assembly than the scripting assembly, the assemblys cannot be referenced, because its a plugin

and i replaced the dictionary with property bag but same: [undefined]
Coordinator
Jan 2, 2015 at 4:21 PM
In our testing, the following works as expected:
var bag = new PropertyBag();
bag["blub"] = 123.456;
engine.AddHostObject("bag", bag);
Debug.Assert(engine.Evaluate("bag['blub']").Equals(123.456));
If you're doing something different, please provide more information.
Jan 2, 2015 at 5:40 PM
im parsing the query of the requesting url, and add the Get class instance to the engine
Coordinator
Jan 2, 2015 at 7:11 PM
Can you provide any details? We can't help if we can't reproduce the problem. Possibly useful might be the relevant parts of the Get class you're using and the code that populates it and exposes it to the script engine. Thanks!
Jan 3, 2015 at 10:28 AM
engine.Add("_GET", Get.Create(p));
 public static PropertyBag Create(HttpProcessor p)
        {
            var props = new PropertyBag();

            var uri = new Uri("htt://localhost" + p.http_url);
            var q = uri.Query;
            foreach (var qi in q.Split(new[] { '&', '?' }, StringSplitOptions.RemoveEmptyEntries))
            {
                var s = qi.Split('=');
                props.Add(s[0], s[1]);
            }

            return props;
        }
Jan 4, 2015 at 11:01 AM
ive fixed, i have used the map class from another discussion
Coordinator
Jan 4, 2015 at 6:35 PM
Glad to hear it furesoft, but PropertyBag really should be sufficient for this purpose.

We don't have your HttpProcessor class, but if we modify the Create method as follows:
public static PropertyBag Create(string url) {
    var props = new PropertyBag();
    var q = new Uri("htt://localhost/" + url).Query;
    foreach (var qi in q.Split(new[] { '&', '?' }, StringSplitOptions.RemoveEmptyEntries)) {
        var s = qi.Split('=');
        props.Add(s[0], s[1]);
    }
    return props;
}
Then this test code works as expected:
engine.AddHostObject("_GET", Create("qux.html?foo=123&bar=456&baz=789"));
engine.AddHostType("Console", typeof(Console));
engine.Execute(@"
    Console.WriteLine(_GET['foo']); // prints '123'
    Console.WriteLine(_GET['bar']); // prints '456'
    Console.WriteLine(_GET['baz']); // prints '789'
");
Good luck!