![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Trouble using MSScript control with Arrays. I'm hosting the MSScript control in a bit of c#, then using AddObject to provide the script with a refenece to a .net object which happens to have an array field. I'm having trouble manipulating this array property, in particular accessing the array elements. Below is a sample bit of c#, where the script control is instantiated, then passed a "person" object which looks like so: public class Person { public string Name = "Fred"; public object[] Possessions = new object [] { "Books", "Pens" }; } This object and it's properties is usable from the script, except that I dont seem to be able to access the array property elements. Specifically, on executing the following vbscript expressions, they all work except the last one: Person.Name & " exists " "Type of host array is " & TypeName(Person.Possessions) "UBound of host array is " & cstr(UBound(Person.Possessions)) "Element of host array is " & cstr(Person.Possessions(1)) This last statement accessing an array element fails with: "Wrong number of arguments or invalid property assignment: 'Person.Possessions'" Any idea why? Or whether there are any known problems accessing arrays contained within a host from withing a hosted script control? (If there's a better place to ask such questions then please advise.) thanks Michael --------------- Here's the code I was using: using System; public class TestScriptControl { public class Person { public string Name = "Fred"; public object[] Possessions = new object [] { "Books", "Pens" }; } static public void Main() { MSScriptControl.IScriptControl scriptControl = new MSScriptControl.ScriptControlClass(); scriptControl.Language = "VBScript"; Person p = new Person(); scriptControl.AddObject("Person", p, false); Console.WriteLine(scriptControl.Eval("Person.Name & \" exists \"")); Console.WriteLine(scriptControl.Eval("\"Type of host array is \" & TypeName(Person.Possessions) ")); Console.WriteLine(scriptControl.Eval("\"UBound of host array is \" & cstr(UBound(Person.Possessions)) ")); Console.WriteLine(scriptControl.Eval("\"Element of host array is \" & cstr(Person.Possessions(1)) ")); } } When run this produces: Fred exists Type of host array is Variant() UBound of host array is 1 Unhandled Exception: System.ArgumentException: Wrong number of arguments or invalid property assignment: 'Person.Possessions' at MSScriptControl.ScriptControlClass.Eval(String Expression) at TestScriptControl.Main() |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Trouble using MSScript control with Arrays. "MichaelL" <google@xxxxxx> wrote in message news:b290ed75-8386-48fc-aaa9-c13aedf0986c@xxxxxx Quote: > I'm hosting the MSScript control in a bit of c#, then using AddObject > to provide the script with a refenece to a .net object which happens > to have an array field. > > I'm having trouble manipulating this array property, in particular > accessing the array elements. > > Below is a sample bit of c#, where the script control is instantiated, > then passed a "person" object which looks like so: > public class Person > { > public string Name = "Fred"; > public object[] Possessions = new object [] { "Books", > "Pens" }; > } > > This object and it's properties is usable from the script, except that > I dont seem to be able to access the array property elements. > > Specifically, on executing the following vbscript expressions, they > all work except the last one: > > Person.Name & " exists " > "Type of host array is " & TypeName(Person.Possessions) > "UBound of host array is " & cstr(UBound(Person.Possessions)) > "Element of host array is " & cstr(Person.Possessions(1)) > > This last statement accessing an array element fails with: > "Wrong number of arguments or invalid property assignment: > 'Person.Possessions'" > > Any idea why? Or whether there are any known problems accessing > arrays contained within a host from withing a hosted script control? > > (If there's a better place to ask such questions then please advise.) > > thanks > Michael > > --------------- > > > > > Here's the code I was using: > > using System; > public class TestScriptControl > { > public class Person > { > public string Name = "Fred"; > public object[] Possessions = new object [] { "Books", > "Pens" }; > } > > static public void Main() > { > MSScriptControl.IScriptControl scriptControl = new > MSScriptControl.ScriptControlClass(); > scriptControl.Language = "VBScript"; > > Person p = new Person(); > scriptControl.AddObject("Person", p, false); > > Console.WriteLine(scriptControl.Eval("Person.Name & \" exists > \"")); > Console.WriteLine(scriptControl.Eval("\"Type of host array is > \" & TypeName(Person.Possessions) ")); > Console.WriteLine(scriptControl.Eval("\"UBound of host array > is \" & cstr(UBound(Person.Possessions)) ")); > Console.WriteLine(scriptControl.Eval("\"Element of host array > is \" & cstr(Person.Possessions(1)) ")); > } > } > > When run this produces: > Fred exists > Type of host array is Variant() > UBound of host array is 1 > > Unhandled Exception: System.ArgumentException: Wrong number of > arguments or invalid property assignment: 'Person.Possessions' > at MSScriptControl.ScriptControlClass.Eval(String Expression) > at TestScriptControl.Main() > Person.Possessions(1).Name would work. The items in the array/collection might be objects. -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net -- |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Arrays | PowerShell | |||
| Control panel trouble. | Vista General | |||
| Trouble Disabling User Account Control | Vista account administration | |||
| Control Arrays | PowerShell | |||