如何将 C# 字符串数组编组为 VB6 数组?
我有一个使用 C# COM DLL 的 VB6 应用程序。在托管 C++ 中,我可以编写一个函数,如下所示:
array<String^>^ GetAManagedArray()
{
//Do stuff and return a managed array
}
然后,我可以将返回的托管数组分配给 VB6 中的数组:
Sub MySub()
Dim strArray() As String
strArray = myComObject.GetAManagedArray
End Sub
这在 C++ 中工作正常,但在 C# 中,System.Array
对象是抽象的,我似乎找不到与 C++ array
等效的托管对象。另外,在 C# 中,仅返回 string[]
是行不通的。
C# 中的托管数组等效项是什么?
编辑:这是我的功能的确切代码
C# COM 函数:
public string[] OneTwoThree()
{
return new string[] { "1", "2", "3" };
}
VB6 函数:
Private Sub Form_Load()
Dim test As New ComObjectCSharp
Dim strArr(), strTemp As String
strArr = test.OneTwoThree
strTemp = strArr(0) & " " & strArr(1) & " " & strArr(2)
MsgBox strTemp
End Sub
代码在 VB6 代码的第四行失败,并出现错误 “编译错误:无法分配给数组”
I have a VB6 app that uses a C# COM DLL. In managed C++ I can write a function as follows:
array<String^>^ GetAManagedArray()
{
//Do stuff and return a managed array
}
I can then assign the returned managed array to an array in VB6:
Sub MySub()
Dim strArray() As String
strArray = myComObject.GetAManagedArray
End Sub
This works fine in C++, but in C# the System.Array
object is abstract and I can't seem to find the managed equivalent to the C++ array<>^
. Also, in C# just returning string[]
does not work.
What is the managed array equivalent in C#?
EDIT: Here is the exact code I have for the fucntions
The C# COM function:
public string[] OneTwoThree()
{
return new string[] { "1", "2", "3" };
}
The VB6 function:
Private Sub Form_Load()
Dim test As New ComObjectCSharp
Dim strArr(), strTemp As String
strArr = test.OneTwoThree
strTemp = strArr(0) & " " & strArr(1) & " " & strArr(2)
MsgBox strTemp
End Sub
The code fails on the fourth line of the VB6 code with the error "Compile error: Can't assign to array"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
strArr() 变量实际上不是字符串数组。它是一系列变体。修复:
现在它与您的第一个片段相同。
The strArr() variable is not actually an array of strings. It is an array of variants. Fix:
Now it is the same as your first snippet.
如果您正在寻找 C# 语法来创建数组,则可以使用
new string[5]
来创建长度为 5 的数组,并使用 null 进行初始化或new string[] { "one", " Two" }
对于具有指定值的数组。除此之外,您必须更具体地说明什么“不起作用”,我们才能帮助您。
If you're looking for the C# syntax to create an array it's
new string[5]
for an array of length 5, initialised with nulls ornew string[] { "one", "two" }
for an array with the specified values.Aside from that, you'd have to be more specific about what "doesn't work" for us to help you.
我的猜测是 test.OneTwoThree 返回单个字符串而不是字符串数组。
尝试使用
Dim strArr
而不是Dim strArr()
如果可行,您必须找出 C# 函数代理的分隔符是什么
返回(有时数组的编组不需要在客户端中获得正确/预期的终止
代码环境)
My guess would be test.OneTwoThree returns a single string not an array of strings.
Try
Dim strArr
instead ofDim strArr()
If that works you have to find out what is the delimiter that the proxy for the C# function
returned (sometimes marshaling of arrays doesn't necessary get proper/expected termination in client
code enviournment)