Com Interop Question and IEnumerator

T

Terry

I have made up a contrived example show the problem I am having. I have some
ReadOnly interfaces:
Public Interface IROPerson
ReadOnly Property FirstName() As String
ReadOnly Property LastName() As String
ReadOnly Property Friends() As IROPersons
End Interface

Public Interface IROPersons
Function Count() As Integer
Function GetEnumerator() As System.Collections.IEnumerator
Function Item(ByVal Index As Integer) As IROPerson
End Interface

On the project Assembly page, I have checked "Make Com Visible". All is
fine so far.

In a new Project, I define the R/W versions. Won't bother you with the
Person class, but here is the 'Persons' class:

Public Class Persons
Implements System.Collections.IEnumerable
Implements ROInterfaces.IROPersons

Private _Persons As New System.Collections.Generic.List(Of Person)

Public Sub Add(ByVal p As Person)
_Persons.Add(p)
End Sub

Public Function Item(ByVal Index As Integer) As Person
Return _Persons(Index)
End Function

Public Function GetEnumerator() As System.Collections.IEnumerator
Implements _
System.Collections.IEnumerable.GetEnumerator, _
ROInterfaces.IROPersons.GetEnumerator
Return _Persons.GetEnumerator
End Function

Public Function Count() As Integer Implements
ROInterfaces.IROPersons.Count
Return _Persons.Count
End Function

Public Function Item1(ByVal Index As Integer) As ROInterfaces.IROPerson
Implements _
ROInterfaces.IROPersons.Item
Return Item(Index)
End Function
End Class

Still so good so far.

I then have a 'Wrapper' then instanciates 'People' who derive from Person.

The wrapper hands back to the 'client' (VB6, VBA, .Net) an IROPerson
reference to a 'Person'.
No problem with a .Net client, but a VB6 client can not 'enumerate' the
'Friends' list of the 'Person'. It's visible, it can go through it with
For...Next Loop but it can't use a For...Each loop.
I have discovered by trial and error, that if I make the "Persons" class a
com class, it works! The client still holds no reference to the com visible
"Persons" class, but somehow this makes 'enumeration' work in the client.
So my question is .... Why?
 
W

Walter Wang [MSFT]

Hi Terry,

Based on my understanding, the ComClassAttribute only helps you to reduce
the necessary code when you need to fine control the GUIDs, define the
interface type, etc. It will not help you implement a necessary interface
that you need to implement to support For...Each enumeration in VB6 or
VBScript: a method with DISPID_NEWENUM (-4).

Here's the code that works in VB6:

Public Interface IROPerson
ReadOnly Property FirstName() As String
ReadOnly Property LastName() As String
ReadOnly Property Friends() As IROPersons
End Interface

Public Interface IROPersons
Function Count() As Integer
<DispId(-4)> _
Function GetEnumerator() As System.Collections.IEnumerator
<DispId(0)> _
Function Item(ByVal Index As Integer) As IROPerson
End Interface

Public Class Test
Public Function Test() As Person
Dim p As New Person
Dim p2 As New Person
p.AddFriend(p2)
Return p
End Function
End Class

<ClassInterface(ClassInterfaceType.None)> _
Public Class Person
Implements IROPerson

Private m_FirstName As String
Private m_Friends As New Persons
Private m_LastName As String

Friend Sub AddFriend(ByVal f As Person)
m_Friends.Add(f)
End Sub

Public ReadOnly Property FirstName() As String Implements
IROPerson.FirstName
Get
Return m_FirstName
End Get
End Property

Public ReadOnly Property Friends() As IROPersons Implements
IROPerson.Friends
Get
Return m_Friends
End Get
End Property

Public ReadOnly Property LastName() As String Implements
IROPerson.LastName
Get
Return m_LastName
End Get
End Property
End Class

<ClassInterface(ClassInterfaceType.None)> _
Public Class Persons
Implements IROPersons
Implements System.Collections.IEnumerable

Private _Persons As New System.Collections.Generic.List(Of Person)

Public Sub Add(ByVal p As Person)
_Persons.Add(p)
End Sub

Public Function Item(ByVal Index As Integer) As Person
Return _Persons(Index)
End Function

Public Function GetEnumerator() As System.Collections.IEnumerator
Implements System.Collections.IEnumerable.GetEnumerator,
IROPersons.GetEnumerator
Return _Persons.GetEnumerator
End Function

Public Function Count() As Integer Implements IROPersons.Count
Return _Persons.Count
End Function

Public Function Item1(ByVal Index As Integer) As IROPerson Implements
IROPersons.Item
Return Item(Index)
End Function
End Class


Some notes:

1. The class Persons implements two interfaces, we need to make IROPersons
the first one, this will make this the default interface of the coclass.

2. Since we intend to let the com clients to use the interfaces IROPerson
and IROPersons, we should instruct the compiler to not to generate another
class interface for the classes, hence the attribute
<ClassInterface(ClassInterfaceType.None)

Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

Terry

Hi Walter,
Thanks for the reply. We the information you provided I was able to get
the original project (as it stands) to work. Now here is a bit of wierdness
for you. Several hours of fooling around yesterday with the concocted
example I posted and it suddenly started working! I have no idea how or why
- but without any of the suggestions that you provided (no dispid's no
nothing different) - it works! Went back to the original project, made sure
it looked exactly (logically) the same and it did not work! Spent bookoo
hours pulling out my hair, trying to figure out why to no avail. Maybe it
has something to do with the implicit ordering of things based on their names
- that is the only difference (that I can find) between the 2 projects.
Anyway, the suggestion you provided fixed the original problem so thank you.

--
Terry


"Walter Wang [MSFT]" said:
Hi Terry,

Based on my understanding, the ComClassAttribute only helps you to reduce
the necessary code when you need to fine control the GUIDs, define the
interface type, etc. It will not help you implement a necessary interface
that you need to implement to support For...Each enumeration in VB6 or
VBScript: a method with DISPID_NEWENUM (-4).

Here's the code that works in VB6:

Public Interface IROPerson
ReadOnly Property FirstName() As String
ReadOnly Property LastName() As String
ReadOnly Property Friends() As IROPersons
End Interface

Public Interface IROPersons
Function Count() As Integer
<DispId(-4)> _
Function GetEnumerator() As System.Collections.IEnumerator
<DispId(0)> _
Function Item(ByVal Index As Integer) As IROPerson
End Interface

Public Class Test
Public Function Test() As Person
Dim p As New Person
Dim p2 As New Person
p.AddFriend(p2)
Return p
End Function
End Class

<ClassInterface(ClassInterfaceType.None)> _
Public Class Person
Implements IROPerson

Private m_FirstName As String
Private m_Friends As New Persons
Private m_LastName As String

Friend Sub AddFriend(ByVal f As Person)
m_Friends.Add(f)
End Sub

Public ReadOnly Property FirstName() As String Implements
IROPerson.FirstName
Get
Return m_FirstName
End Get
End Property

Public ReadOnly Property Friends() As IROPersons Implements
IROPerson.Friends
Get
Return m_Friends
End Get
End Property

Public ReadOnly Property LastName() As String Implements
IROPerson.LastName
Get
Return m_LastName
End Get
End Property
End Class

<ClassInterface(ClassInterfaceType.None)> _
Public Class Persons
Implements IROPersons
Implements System.Collections.IEnumerable

Private _Persons As New System.Collections.Generic.List(Of Person)

Public Sub Add(ByVal p As Person)
_Persons.Add(p)
End Sub

Public Function Item(ByVal Index As Integer) As Person
Return _Persons(Index)
End Function

Public Function GetEnumerator() As System.Collections.IEnumerator
Implements System.Collections.IEnumerable.GetEnumerator,
IROPersons.GetEnumerator
Return _Persons.GetEnumerator
End Function

Public Function Count() As Integer Implements IROPersons.Count
Return _Persons.Count
End Function

Public Function Item1(ByVal Index As Integer) As IROPerson Implements
IROPersons.Item
Return Item(Index)
End Function
End Class


Some notes:

1. The class Persons implements two interfaces, we need to make IROPersons
the first one, this will make this the default interface of the coclass.

2. Since we intend to let the com clients to use the interfaces IROPerson
and IROPersons, we should instruct the compiler to not to generate another
class interface for the classes, hence the attribute
<ClassInterface(ClassInterfaceType.None)

Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Terry,

Please note that if you're not explicitly setting GUIDs for your interfaces
and classes (using GuidAttribute), the compiler will generate one for you;
and these generated GUIDs will change if you add new public
methods/properties to the interfaces. If you have checked option "Register
for COM interop" in build options, that means some invalid GUIDs are
written into registry and it might have some unexpected issues.

You can try your components on a fresh system to see if it works or not.

The recommended approach is to use GuidAttribute to explicitly specify
GUIDs to use; or you can use ComClassAttribute (and the Com Class template)
to simply the code.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

FYI:

Adam Nathan's Blog : GUID Generation and VB6 Binary Compatibility
(http://blogs.msdn.com/adam_nathan/archive/2003/10/19/56779.aspx)

This blog has complete explanation on the auto generated GUIDs.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top