gpt4 book ai didi

c# - 如何从 COM 客户端访问基于列表(Of T)的对象

转载 作者:太空宇宙 更新时间:2023-11-03 16:48:22 25 4
gpt4 key购买 nike

全部

我在 VS2005 中编写的内容,从表面上看应该是一个相对简单的 DLL,它允许我在从 Excel 2007 中调用时构建自定义对象列表。为此,我有一个 Column 和 Columns类(class)。正如暗示的那样,Columns 是 Column 对象的列表。相关类定义如下:

<Guid("1CD713EC-D140-4e8b-92D7-99E098694A85")> _
<InterfaceType(ComInterfaceType.InterfaceIsDual)> _
Public Interface IColumn
Property Name() As String
End Interface
<Guid("6B146BF8-E905-4ed1-84D5-147B1A510AE3")> _
<ClassInterface(ClassInterfaceType.None)> _
Public Class Column
Implements IColumn
Private _name As String
Public Sub New()
MyBase.new()
End Sub
Public Property Name() As String Implements IColumn.Name
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
End Class

<Guid("C2C98A9B-DF7B-4ce1-9F48-EF2B0E63A8E8")> _
<InterfaceType(ComInterfaceType.InterfaceIsDual)> _
Public Interface IColumns
Sub Add(ByVal col As Column)
End Interface

<Guid("CA76EAF0-1B87-402a-8720-933EDFA0CAE4")> _
<ClassInterface(ClassInterfaceType.None)> _
Public Class Columns
Inherits List(Of Column)
Implements IColumns
Public Sub New()
MyBase.New()
End Sub
Public Overloads Sub Add(ByVal col As Column) Implements IColumns.Add
col.Name = col.Name.ToUpper
MyBase.Add(col)
End Sub
End Class

以下代码在 VS2005 中按预期工作:

Dim col As Column
Dim cols As New Columns
col = New Column
col.Name = "DefType"
cols.Add(col)

但是,当使用 VBA 从 Excel 中调用时:

Dim col As PiMdbManager.Column
Dim cols As PiMdbManager.Columns
Dim cnt As Integer
Dim colName As String

head = header.Value

For cnt = 0 To header.Count - 1
colName = header(1, cnt + 1)
If colName <> "" Then
Set col = New PiMdbManager.Column
col.Name = header(1, cnt + 1)
cols.Add (col)
End If
Next cnt

“cols.Add (col)”行生成错误“对象不支持此属性或方法”。 .Add 方法通过智能感知在 VBA 中可见。

有人可以向我解释这里发生了什么,以及我如何能够实现我想要做的事情。

亲切的问候

保罗·J。

最佳答案

尝试在 cols.add (col) 中删除 VBA 中的括号,使其看起来像这样:cols.Add col

或者您可以使用“调用”关键字:

Call cols.Add (col)

关于c# - 如何从 COM 客户端访问基于列表(Of T)的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5079038/

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