473,395 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 2452
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Sasha | last post by:
Hi, I am extending standard IEnumerator, and I was just wondering what is the best way to make enumarator safe? What do I mean by safe? Detect deletes and all... My idea is to have private Guid...
1
by: juan | last post by:
hi i have a teachers class where i want the user to enter a few teachers and iterate thru them. In the main i get an error System.Collections.IEnumerator' does not contain a definition for...
3
by: Hans | last post by:
I implemented the IEnumerator interface in some classes to enable the use of the foreach statement. As you probalbly know, the interface asks for the implementation of object IEnumerator.Curren...
4
by: Benjamin Piorczig | last post by:
Hello list memebers, I'd like to use a C# Collection with from within Visual Basic 6. I have derived a my ComInterface class from IList and implemented those Members. Everything seems to work...
3
by: starter | last post by:
I am trying to learn IEnumerator and collections(ArrayList, Hashtable) in .NET. Can anyone tell me what is IEnumerator used for and any online resources would be helpful. Thanks
1
by: midnight madness | last post by:
I tried but failed to implement a template class that support IEnumerator<T> interface using C++/CLI in VS 2005 Professional version. I could not figure out the proper syntax to implement the...
11
by: Leslie Sanford | last post by:
I've been kicking around an idea mostly as a thought experiment at this point. The idea is to create a thread safe collection that implements the IEnumerable interface. The enumerators it creates...
5
by: Shikari Shambu | last post by:
Hi, I am trying to implement a collection that implements IEnumerable<T>. I keep getting the following error 'IEnumerator<...>.Current' in explicit interface declaration is not a member of...
3
by: EmilH | last post by:
Hi. I have read some examples of IEnumerator at http://www.codeproject.com/csharp/csenumerators.asp What if I have 2 arrays or collections, for example a DataSet which has 2 DataTables in my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.