[英]Double parentheses in VB6 Array syntax (passed from .Net com-interop)
I have a C# dll exposed to vb6 via com-interop. 我有一个C#dll通过com-interop暴露给vb6。 This is all working, but I am noticing something strange when I pass an array of a custom objects from .Net into VB6. 这一切都有效,但是当我将.Net的自定义对象数组传递给VB6时,我注意到了一些奇怪的事情。
Accessing the array from VB6 is what baffles me. 从VB6访问数组让我感到困惑。 If I access the array directly I have to do it like this: 如果我直接访问数组,我必须这样做:
Dim manager as New ObjectManager
'Access with two sets of parentheses:
msgbox manager.ReturnArrayOfObjects()(0).Name
However, if I copy the array first I can access it how I would normally expect to: 但是,如果我先复制数组,我可以访问它通常所期望的:
Dim manager as New ObjectManager
Dim objectArray() As CustomObject
'copy the array
objectArray = manager.ReturnArrayOfObjects
'access normally:
msgbox objectArray(0).Name
In the first case I had to use two sets of parentheses: manager.ReturnArrayOfObjects()(0).Name
In the second case I could just use one set of parentheses: objectArray(0).Name
在第一种情况下,我不得不用两套括号: manager.ReturnArrayOfObjects()(0).Name
在第二种情况下,我可以只使用一组括号: objectArray(0).Name
Does anyone know why this is the case? 有谁知道为什么会这样? Am I doing something wrong here with the interop maybe? 我可能在这里做错了吗?
Here is a quick stub/sample of the C# interop code. 这是C#interop代码的快速存根/示例。
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("[Guid here...]")]
public interface IObjectManager
{
[DispId(1)]
CustomObject[] ReturnArrayOfObjects();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("[guid here...]")]
public class ObjectManager: IObjectManager
{
public CustomObject[] ReturnArrayOfObjects()
{
return new CustomObject[] { new CustomObject(), new CustomObject() };
}
}
The class CustomObject()
is also exposed to com-interop and working just fine. CustomObject()
类也暴露给com-interop并且正常工作。 Please let me know if you need me to post anymore code, but I think these little snippets represent the problem well enough to begin with. 如果您需要我发布更多代码,请告诉我,但我认为这些小片段代表了这个问题。
Thanks in advance for your help. 在此先感谢您的帮助。
ReturnArrayOfObjects()
in the C# code is a method. C#代码中的ReturnArrayOfObjects()
是一种方法。 Your VB6 code is invoking the method, which returns the array, and then accessing the first element. 您的VB6代码正在调用该方法,该方法返回数组,然后访问第一个元素。 The difference between this 这个区别
msgbox manager.ReturnArrayOfObjects()(0).Name
and this 和这个
objectArray = manager.ReturnArrayOfObjects
msgbox objectArray(0).Name
Is that in the second, you invoke the method by itself without accessing the first element, and VB is allowing you to leave off the parentheses from the method call. 在第二个中,您在不访问第一个元素的情况下自己调用方法,VB允许您从方法调用中省略括号。 Conversely, the language is not allowing you to leave off the parentheses when you directly access the first element. 相反,当您直接访问第一个元素时,该语言不允许您放弃括号。 It's simply a language feature, it's not a "double parentheses array syntax" issue. 它只是一种语言功能,它不是“双括号数组语法”问题。
ReturnArrayOfObjects
is a method, that must be called. ReturnArrayOfObjects
是一个必须被调用的方法。 In VB6, if you're calling a method and supplying no parameters, and it's the entire statement, then you can omit the parenthesis. 在VB6,如果你调用一个方法并提供任何参数,它是整个语句,那么你可以省略括号。
However, in your first example, you're calling the method, and then indexing into the array returned by that method. 但是,在第一个示例中,您将调用该方法,然后索引到该方法返回的数组。 You need the first set of parenthesis to indicate that you're passing no parameters to the method, and then the second set of parenthesis are being used for array indexing. 您需要第一组括号表示您没有向方法传递任何参数,然后第二组括号用于数组索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.