Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Trouble using MSScript control with Arrays.

Reply
 
Old 05-27-2009   #1 (permalink)
MichaelL


 
 

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 SpecsSystem Spec
Old 05-27-2009   #2 (permalink)
Richard Mueller [MVP]


 
 

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()
>
Just a guess out of the blue, but I wonder if Person.Possessions(1).Value or
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 SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Arrays PowerShell
Control panel trouble. Vista General
Trouble Disabling User Account Control Vista account administration
Control Arrays PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46