I have a .NET class which holds a simple array of strings available via an accessor method, which looks like this;
我有一个。net类,它通过访问器方法保存了一个简单的字符串数组,如下所示;
namespace Foo.Bar {
[ComVisible(true)]
[Guid("642279A0-85D4-4c7a-AEF5-A9FAA4BE85E5")]
public class MyClass {
private string[] _myArray;
public MyClass() { }
public MyClass(string[] myArray) {
_myArray = myArray;
}
public string[] MyArray {
get { return _myArray; }
}
}
}
I consume this class using Classic ASP;
我使用经典的ASP来使用这个类;
Dim foo
Set foo = Server.CreateObject("Foo.Bar.MyClass")
if IsArray(foo.MyArray) then Response.Write("IsArray") & "<br />"
Response.Write(typename(foo.MyArray)) & "<br />"
Response.Write(UBound(foo.MyArray)) & "<br />"
This results in;
这导致;
IsArray
String()
1
However, when I try to access the contents of the array using;
但是,当我尝试使用;
Response.Write(foo.MyArray(0)) & "<br />"
I get;
我得到;
Microsoft VBScript runtime (0x800A01C2) Wrong number of arguments or invalid property assignment: 'MyArray'
Microsoft VBScript运行时(0x800A01C2)参数数错误或属性分配无效:“MyArray”
Any help is much appreciated.
非常感谢您的帮助。
Edit This is to provide more information after digesting the answers given (thanks)
编辑这是为了提供更多的信息,在消化了答案后(谢谢)
When changing the implementation of the MyArray property to;
将MyArray属性的实现更改为;
public object[] MyArray {
get { return (object[])_myArray; }
}
I then get the following error,
然后得到下面的错误,
Microsoft VBScript runtime (0x800A000D) Type mismatch: 'MyArray'
Microsoft VBScript运行时(0x800A000D)类型不匹配:“MyArray”
So I tried individually casting each string to an object;
所以我试着把每根弦都投射到一个物体上;
public object[] MyArray {
get {
object[] tmp = new object[_myArray.Count()];
for (int x = 0; x < _myArray.Count(); x++) {
tmp[x] = (object)_myArray[x];
}
return tmp;
}
}
Then I'm back to,
然后我回来了,
Microsoft VBScript runtime (0x800A01C2) Wrong number of arguments or invalid property assignment: 'MyArray'
Microsoft VBScript运行时(0x800A01C2)参数数错误或属性分配无效:“MyArray”
Edit Final solution with help from How to correctly marshal VB-Script arrays to and from a COM component written in C#
编辑最终的解决方案,帮助您如何正确地编列vb脚本阵列和从c#编写的COM组件。
C#
c#
public object MyArray {
get { return _myArray.Cast<object>().ToArray(); }
}
VBScript
VBScript
Dim foo
Set foo = Server.CreateObject("Foo.Bar.MyClass")
bar = foo.MyArray
Response.Write bar(0)
The key was to expose object
rather than object[]
and as AnthonyWJones suggested, assign the array to a variable before using it.
关键是公开对象而不是对象[],正如AnthonyWJones所建议的,在使用数组之前将数组分配给一个变量。
Thanks again.
再次感谢。
3 个解决方案
#1
2
The problem is VBScript cannot actually use an array of String
. It can only use an array of Variant
.
问题是VBScript实际上不能使用字符串数组。它只能使用一个变体数组。
Try changing MyClass
to expose an object[]
instead.
尝试更改MyClass以公开一个对象[]。
#2
1
In addition to Anthony's suggestion
I'm not sure is it the best way but in the past I used a code similar to the following to handle one dimensional arrays.
除了Anthony的建议,我不确定这是否是最好的方法,但在过去,我使用了类似于下面的代码来处理一维数组。
public object MyArray(int ix = -1){
string[] tmp = new string[] {"one", "two", "3", "4"};
return (ix == -1) ? (object)tmp : tmp[ix];
}
In ASP:
在ASP:
Response.Write(TypeName(foo.MyArray)) 'string()
Response.Write(TypeName(foo.MyArray(0))) 'string
#3
-1
This code demonstrates how to handle arrays between COM and ASP:
这段代码演示了如何处理COM和ASP之间的数组:
<% @Language="VBScript" %>
<% Option Explicit %>
<%
Dim tcs
Dim rc
Dim vntInput(0,4)
Dim i
vntInput(0,0) = Request.QueryString("strUser")
vntInput(0,1) = Request.QueryString("intCreate")
vntInput(0,2) = Request.QueryString("intDelete")
vntInput(0,3) = Request.QueryString("intModify")
vntInput(0,4) = Request.QueryString("intView")
Set tcs = Server.CreateObject("TestCases.ArrayFailure")
rc = tcs.AcceptArray(vntInput)
For i = 0 to UBound(vntInput, 2)
Response.write "Loop Count " & i & " " & vntInput(0,i) & "<BR>"
Next
%>
Here's a link to the article where I found this code:
http://202.102.233.250/b2000/ASP/articles/component/pv990826.htm
下面是我找到这段代码的文章的链接:http://202.102.233.250/b2000/ASP/articles/component/pv990826.htm
#1
2
The problem is VBScript cannot actually use an array of String
. It can only use an array of Variant
.
问题是VBScript实际上不能使用字符串数组。它只能使用一个变体数组。
Try changing MyClass
to expose an object[]
instead.
尝试更改MyClass以公开一个对象[]。
#2
1
In addition to Anthony's suggestion
I'm not sure is it the best way but in the past I used a code similar to the following to handle one dimensional arrays.
除了Anthony的建议,我不确定这是否是最好的方法,但在过去,我使用了类似于下面的代码来处理一维数组。
public object MyArray(int ix = -1){
string[] tmp = new string[] {"one", "two", "3", "4"};
return (ix == -1) ? (object)tmp : tmp[ix];
}
In ASP:
在ASP:
Response.Write(TypeName(foo.MyArray)) 'string()
Response.Write(TypeName(foo.MyArray(0))) 'string
#3
-1
This code demonstrates how to handle arrays between COM and ASP:
这段代码演示了如何处理COM和ASP之间的数组:
<% @Language="VBScript" %>
<% Option Explicit %>
<%
Dim tcs
Dim rc
Dim vntInput(0,4)
Dim i
vntInput(0,0) = Request.QueryString("strUser")
vntInput(0,1) = Request.QueryString("intCreate")
vntInput(0,2) = Request.QueryString("intDelete")
vntInput(0,3) = Request.QueryString("intModify")
vntInput(0,4) = Request.QueryString("intView")
Set tcs = Server.CreateObject("TestCases.ArrayFailure")
rc = tcs.AcceptArray(vntInput)
For i = 0 to UBound(vntInput, 2)
Response.write "Loop Count " & i & " " & vntInput(0,i) & "<BR>"
Next
%>
Here's a link to the article where I found this code:
http://202.102.233.250/b2000/ASP/articles/component/pv990826.htm
下面是我找到这段代码的文章的链接:http://202.102.233.250/b2000/ASP/articles/component/pv990826.htm