Re: Using vb.net collections in VBA through COMClass

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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 (jialge@xxxxxxxxxxxxxxxxxxxx, 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:
msdnmg@xxxxxxxxxxxxxx

==================================================
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.



.



Relevant Pages

  • Re: Using vb.net collections in VBA through COMClass
    ... Public Sub New ... Public ReadOnly Property Salary() As Decimal ... Dim coll As New Employees ... Microsoft Online Community Support ...
    (microsoft.public.excel.programming)
  • For Iman the caps civilian, amongst me its jolly, whereas aged you its cracking unpleasant.
    ... terrible respective employees will let_'s tensely in respect of ... You concede the balanced fighting and support it ... electoral concerning her equivalent geography. ... capitalist expense very highly? ...
    (sci.crypt)
  • U2/DataFlo Business Programmer/Analyst - Pullman, WA (USA)
    ... developer to join our current team-of-four to support company growth. ... SEL has more than 2000 employees and is 100% employee- ... Each business programmer/analyst participates in user support, ... SEL is one of the few that proves it daily. ...
    (comp.databases.pick)
  • Re: (NBC) Wal-Mart Dont Want Obama to Win
    ... implication from company heads that employees should support ... Democrats, or not support Republicans. ... Now, there IS another way to look at it, of course: Google employees might ...
    (rec.music.artists.springsteen)
  • Re: Northwind ReportsTo NULL ComboBox
    ... The select statement that is used by the DataAdapter for the Employees ... database table simply reads all records: ... >>case where ReportsTo is NULL. ... >>Public Sub NULLToZero(ByVal sender As Object, ...
    (microsoft.public.dotnet.framework.adonet)