Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: Problem with C# enumerables (generic, arrays, whatever) with vbscript

0
0
Greetings!

The List class has an indexed property named "Item". This property is exposed as a C# indexer, but you can use it from script in its true form:
engine.AddHostObject("list", new List<string> { "abc", "def", "ghi", "jkl" });
engine.AddHostType("Console", typeof(Console));
engine.Execute(@"
    for i = 0 to list.Count - 1
        Console.WriteLine list.Item(i)
    next
");
The ElementAt method is a LINQ extension. To use it, you must expose LINQ's Enumerable class:
engine.AddHostType("Enumerable", typeof(Enumerable));
engine.Execute(@"
    for i = 0 to list.Count - 1
        Console.WriteLine list.ElementAt(i)
    next
");
Another thing you can do is use a .NET enumerator:
engine.Execute(@"
    e = list.GetEnumerator()
    while e.MoveNext()
        Console.WriteLine e.Current
    wend
");
Good luck!

Viewing all articles
Browse latest Browse all 2297