Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Documentation question on CsScript. Get List<>, collections.
#5
Hm. This worked in your example but not my code. I think I've found out what the issue is (so my problem is solved) but I don't understand why this makes a difference. First - a working example:
Code:
Copy      Help
Function [b]CSTest[/b] [help1][/help1]
[code]

out

CsScript x.AddCode("")
IDispatch t=x.CreateObject("Test")

;IDispatch k=t.k ;;error
ARRAY(str) b=t.GetListAsArray ;;OK
for(_i 0 b.len) out b[_i]

IDispatch a=t.a ;;OK
out a.Count
out a.Item(0)
a.Item(0)="newstring"
ARRAY(IDispatch) testStrings = t.testStrings
out F"Length of testStrings array {testStrings.len}"
for _i 0 testStrings.len
,out testStrings[_i].aString
,out testStrings[_i].bString
#ret
using System;
using System.Runtime.InteropServices;
using System.Collections;
using System.Collections.Generic;

public class Test2Strings{
,public string aString {get;set;}
,public string bString;
}
public class Test
{
public List<string> k { get; private set;}
public ArrayList a { get; private set;}
public Test2Strings[] testStrings ;//{get; set;}


public Test()
{
k=new List<string>(); k.Add("string in List");
a=new ArrayList(); a.Add("string in ArrayList");
testStrings = new Test2Strings[3];
for (int i=0;i<testStrings.Length; i++){
,testStrings[i] = new Test2Strings();
,testStrings[i].aString = "First String " + i;
,testStrings[i].bString = "Second String " + i;
,}
}

public string[] GetListAsArray() { return k.ToArray(); }
}
[/code]

When I run this I get the output
Code:
Copy      Help
string in List
1
string in ArrayList
Length of testStrings array 3
First String 0
Second String 0
First String 1
Second String 1
First String 2
Second String 2

but if I change
Code:
Copy      Help
public Test2Strings[] testStrings ;//{get; set;}
to
Code:
Copy      Help
public Test2Strings[] testStrings {get; set;}
the output is
Code:
Copy      Help
string in List
1
string in ArrayList
Error (RT) in CSTest:  type mismatch.    ?

This must mean that adding the accessors is fine for simple C# strings and for string[]s - but if there is a IDispatch-ish type that the accessors add some type information that then throws the mismatch. No idea what this is but I can go forward from here now. Thanks!


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)