469,645 Members | 1,424 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

Com Interop Question and IEnumerator

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?
--
Terry
Jan 13 '08 #1
3 2334
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 (wa****@online.microsoft.com, 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.

Jan 14 '08 #2
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 (wa****@online.microsoft.com, 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.

Jan 15 '08 #3
FYI:

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

This blog has complete explanation on the auto generated GUIDs.

Regards,
Walter Wang (wa****@online.microsoft.com, 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.

Jan 22 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by juan | last post: by
3 posts views Thread by Hans | last post: by
4 posts views Thread by Benjamin Piorczig | last post: by
3 posts views Thread by starter | last post: by
1 post views Thread by midnight madness | last post: by
11 posts views Thread by Leslie Sanford | last post: by
5 posts views Thread by Shikari Shambu | last post: by
3 posts views Thread by EmilH | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.