Is there an issue with accessing properties of objects in List<MyClass>

Apr 17, 2010 at 1:50 PM

While I can pass a List<int> to Jint I can not access properties of my classes.  For example:

 

 

 
public class MyClass

{
   public string Description {get; set;} 
   public int Status{get; set;}

   public MyClass(string description, int status)
   {
       this.Description = description;
       this.Status = status;
   }
}

and then
List<MyClass>myClasses = new List<MyClass>();
myClasses.Add(new MyClass("Some text",2));
myClasses.Add(new MyClass("More text", 1));

var jintEngine = new JintEngine();
jintEngine.SetParameters("myClasses", myClasses);

string result = jintEngine.Run("return myClasses[0].Description");
result gets set to null. When trying to get things working, I tested whether the script was setting the object by returning myClasses.Count and I am getting the Count returned:

string result = jintEngine.Run("return myClasses.Count");
Am I missing something? Regardless, this is a great start to something really cool.

 

May 28, 2010 at 3:16 PM

BUMP: I am also having this problem

Apr 1, 2011 at 4:02 PM

I believe I am experiencing the same issue and wondered if this has been resolved.

 

I have a class that has a bunch of strings in it that pass to and from jint when I run a script so the script will have access to those variables.  I recently added a List<string> variable to the class (csharp code)  and tried to access it via js only to get an error.

 

I was not sure if the syntax was correct but the previous posting seems to state I am on the right path.

 

jintEngine.SetParameters("Variables", ExposedVariables);
string result = jintEngine.Run("return Variables.string1"); - this is OK
string result = jintEngine.Run("return Variables.list[0]");  - this does NOT work

Am I approaching this correctly?
Apr 2, 2011 at 5:06 PM

jintEngine.Run(...) returns an object, therefore you can't assign it to a string without having an explicit type conversion. Also 'JintEngine' doesn't have 'SetParameters' method.
Please supply us with full example code, version of jint you are using, since
    public class MyClass
    {
        public string Description { get; set; }
        public int Status { get; set; }

        public MyClass(string description, int status)
        {
            this.Description = description;
            this.Status = status;
        }
    }

    class Program {
        static void Main(string[] args)
        {
            
            List<MyClass>myClasses = new List<MyClass>();
            myClasses.Add(new MyClass("Some text",2));
            myClasses.Add(new MyClass("More text", 1));

            var jintEngine = new JintEngine();
            jintEngine.SetParameter("myClasses", myClasses);

            Console.WriteLine("Result: {0}", jintEngine.Run("return myClasses[0].Description"));
            System.Console.ReadKey();
        }
    }
prints: Result: 
Some text

with latest jint from branch v.0.9.1, and jint 0.9.0 should also work fine.

Sincerely yours,
cin.