Good morning Jason,
You would need to manually expose the Count property from the VB.NET class
that inherits CollectionBase. Here is an example:
====VB.NET class exposed as a COM component====
Imports System.Collections
<ComClass()> _
Public Class Employee
Private m_Name As String
Private m_Salary As Decimal
Public Sub New(ByVal theName As String, ByVal curSalary As Decimal)
m_Name = theName
m_Salary = curSalary
End Sub
Public ReadOnly Property Name() As String
Get
Return m_Name
End Get
End Property
Public ReadOnly Property Salary() As Decimal
Get
Return MyClass.m_Salary
End Get
End Property
End Class
<ComClass()> _
Public Class Employees
Inherits System.Collections.CollectionBase
Public Sub Add(ByVal aEmployee As Employee)
List.Add(aEmployee)
End Sub
Public Sub Remove(ByVal index As Integer)
If index > Count - 1 Or index < 0 Then
Console.WriteLine("Can't add this item")
Else
List.RemoveAt(index)
End If
End Sub
Public Shadows ReadOnly Property Count() As Integer
Get
Return MyBase.Count
End Get
End Property
Default Public ReadOnly Property Item(ByVal index As Integer) As
Employee
Get
Return CType(List.Item(index), Employee)
End Get
End Property
End Class
<ComClass()> _
Public Class SimpleClass
Public Function GetCollection()
Dim coll As New Employees
coll.Add(New Employee("Mike", 10))
coll.Add(New Employee("Mary", 20))
Return coll
End Function
End Class
=====VBA code that consumes the component======
1. add reference to the COM component
2. run the code:
Sub test()
Dim obj As New TestCOMClass.SimpleClass
Dim coll As Employees
Set coll = obj.GetCollection
MsgBox coll.Count
For n = 0 To coll.Count - 1
MsgBox coll.Item(n).Name
Next
End Sub
In this example, I exposed the Count property manually:
Public Shadows ReadOnly Property Count() As Integer
Get
Return MyBase.Count
End Get
End Property
The generated typelib for the GetCollection method is:
interface _SimpleClass : IDispatch {
[id(0x00000001)]
HRESULT GetCollection([out, retval] VARIANT* pRetVal);
};
And the Employees interface looks like:
[
odl,
uuid(60778372-FA23-3B3E-A626-9F6833937782),
version(1.0),
dual,
oleautomation,
custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9,
"TestCOMClass.Employees+_Employees")
]
interface _Employees : IDispatch {
[id(0x00000001)]
HRESULT Add([in] _Employee* aEmployee);
[id(0x00000002)]
HRESULT Remove([in] long index);
[id(0x00000003), propget]
HRESULT Count([out, retval] long* pRetVal);
[id(00000000), propget]
HRESULT Item(
[in] long index,
[out, retval] _Employee** pRetVal);
};
Please let me know whether this example is helpful to you or not.
Best Regards,
Jialiang Ge (
[email protected], remove 'online.')
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.