B
Bob Beaucage
Guest
Hi,
I've created a com visible class in VB.Net, and I want to access it in Excel VBA.
I have a collection as part of the class, but I'm unable to loop through the collection (for each..next).
I added the <DispId(-4)> in front of the GetEnumerator, as suggested by some other posts I've read.
It does say that generic code may not be exported to COM, but I'm not sure what to change to allow it to be exported.
This is done in VS2017.
Thank you in advance.
<ComVisible(True)>
<ClassInterface(ClassInterfaceType.AutoDual)>
Public Class SageMessage
Public Property strMessageID As String
Public Property strMessage As String
End Class
<ComVisible(True)>
<ClassInterface(ClassInterfaceType.AutoDual)>
Public Class SageMessages
Implements IEnumerable(Of SageMessage), IEnumerator(Of SageMessage)
Private msgList As New List(Of SageMessage)
Private index As Integer = -1
Private iCount As Integer
Public Sub Add(message As SageMessage)
msgList.Add(message)
End Sub
Public Function Remove(message As SageMessage) As Boolean
Return msgList.Remove(message)
End Function
<DispId(-4)>
Public Function GetEnumerator() As IEnumerator(Of SageMessage) Implements IEnumerable(Of SageMessage).GetEnumerator
Return msgList.GetEnumerator
End Function
Public Function GetEnumerator1() As System.Collections.IEnumerator Implements IEnumerable.GetEnumerator
Return GetEnumerator()
End Function
<DispId(0)>
Default Public ReadOnly Property Item(index As Integer)
Get
Return msgList(index)
End Get
End Property
Public ReadOnly Property Count()
Get
Return msgList.Count
End Get
End Property
Public ReadOnly Property Current() As SageMessage Implements IEnumerator(Of SageMessage).Current
Get
Return msgList(index)
End Get
End Property
Public ReadOnly Property IEnumerator_Current() As Object Implements IEnumerator.Current
Get
Return Me.Current
End Get
End Property
Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
If index < iCount Then
index = index + 1
MoveNext = True
Else
MoveNext = False
End If
End Function
Public Sub Reset() Implements IEnumerator.Reset
index = -1
End Sub
Public Sub Dispose() Implements IEnumerator(Of SageMessage).Dispose
msgList = Nothing
End Sub
End Class
Continue reading...
I've created a com visible class in VB.Net, and I want to access it in Excel VBA.
I have a collection as part of the class, but I'm unable to loop through the collection (for each..next).
I added the <DispId(-4)> in front of the GetEnumerator, as suggested by some other posts I've read.
It does say that generic code may not be exported to COM, but I'm not sure what to change to allow it to be exported.
This is done in VS2017.
Thank you in advance.
<ComVisible(True)>
<ClassInterface(ClassInterfaceType.AutoDual)>
Public Class SageMessage
Public Property strMessageID As String
Public Property strMessage As String
End Class
<ComVisible(True)>
<ClassInterface(ClassInterfaceType.AutoDual)>
Public Class SageMessages
Implements IEnumerable(Of SageMessage), IEnumerator(Of SageMessage)
Private msgList As New List(Of SageMessage)
Private index As Integer = -1
Private iCount As Integer
Public Sub Add(message As SageMessage)
msgList.Add(message)
End Sub
Public Function Remove(message As SageMessage) As Boolean
Return msgList.Remove(message)
End Function
<DispId(-4)>
Public Function GetEnumerator() As IEnumerator(Of SageMessage) Implements IEnumerable(Of SageMessage).GetEnumerator
Return msgList.GetEnumerator
End Function
Public Function GetEnumerator1() As System.Collections.IEnumerator Implements IEnumerable.GetEnumerator
Return GetEnumerator()
End Function
<DispId(0)>
Default Public ReadOnly Property Item(index As Integer)
Get
Return msgList(index)
End Get
End Property
Public ReadOnly Property Count()
Get
Return msgList.Count
End Get
End Property
Public ReadOnly Property Current() As SageMessage Implements IEnumerator(Of SageMessage).Current
Get
Return msgList(index)
End Get
End Property
Public ReadOnly Property IEnumerator_Current() As Object Implements IEnumerator.Current
Get
Return Me.Current
End Get
End Property
Public Function MoveNext() As Boolean Implements IEnumerator.MoveNext
If index < iCount Then
index = index + 1
MoveNext = True
Else
MoveNext = False
End If
End Function
Public Sub Reset() Implements IEnumerator.Reset
index = -1
End Sub
Public Sub Dispose() Implements IEnumerator(Of SageMessage).Dispose
msgList = Nothing
End Sub
End Class
Continue reading...