gpt4 book ai didi

vb.net - 如何在 VB.NET 中声明 COM 可见的默认索引属性?

转载 作者:行者123 更新时间:2023-12-01 04:21:54 26 4
gpt4 key购买 nike

你如何在 VB.NET 中声明一个默认的索引属性,以便它可以从 VBScript 中调用?

我试过这个使用

<DispId(0)> _
Public ReadOnly Property Item(ByVal idx As Integer) As ...

但 VBScript 返回错误信息

错误数量的参数或无效的属性分配
错误代码 800A01C2

如果我从 VB.NET 公开一个普通属性(非索引),则不会发生此错误。

这是一个示例:

'Class1.vb:
  Public Class Class1
Public ReadOnly Property Dogs() As Dogs
Get
Return New Dogs()
End Get
End Property
End Class

'狗.vb:
  Imports System.Runtime.InteropServices

Public Class Dogs
<DispId(0)> _
Public ReadOnly Property Item(ByVal idx As Integer) As Dog
Get
Return New Dog
End Get
End Property
End Class

'狗.vb:
  Public Class Dog
Public ReadOnly Property Name() As String
Get
Return "Fido"
End Get
End Property
End Class

脚本:
  Set obj = CreateObject("FmuComTest.Class1")

MsgBox obj.Dogs.Item(0).Name ' this works

MsgBox obj.Dogs(0).Name ' error message

访问默认索引属性 Dogs.Item(idx) 会导致错误消息。

最佳答案

这篇 MSDN 博客文章可能对这个问题有所了解。 VBScript 和 COM 似乎对如何调用默认值很挑剔。

http://blogs.msdn.com/ericlippert/archive/2005/08/30/458051.aspx

关于vb.net - 如何在 VB.NET 中声明 COM 可见的默认索引属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1188455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com