473,804 Members | 4,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons.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.Collecti ons.IEnumerable
Implements ROInterfaces.IR OPersons

Private _Persons As New System.Collecti ons.Generic.Lis t(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.Collecti ons.IEnumerator
Implements _
System.Collecti ons.IEnumerable .GetEnumerator, _
ROInterfaces.IR OPersons.GetEnu merator
Return _Persons.GetEnu merator
End Function

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

Public Function Item1(ByVal Index As Integer) As ROInterfaces.IR OPerson
Implements _
ROInterfaces.IR OPersons.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 2470
Hi Terry,

Based on my understanding, the ComClassAttribu te 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.Collecti ons.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 (ClassInterface Type.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.First Name
Get
Return m_FirstName
End Get
End Property

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

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

<ClassInterface (ClassInterface Type.None)_
Public Class Persons
Implements IROPersons
Implements System.Collecti ons.IEnumerable

Private _Persons As New System.Collecti ons.Generic.Lis t(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.Collecti ons.IEnumerator
Implements System.Collecti ons.IEnumerable .GetEnumerator,
IROPersons.GetE numerator
Return _Persons.GetEnu merator
End Function

Public Function Count() As Integer Implements IROPersons.Coun t
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 (ClassInterface Type.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 ComClassAttribu te (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
1845
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 state field in the collection, and every time something is inserted or deleted from the collection, I will just change the guid. Enumerator will just have to compare the guid received in the begging to the current one. If they are different, the...
1
2126
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 'Length' why is that? is this approach right? I want to develop an OO App. so is this approach right?
3
7125
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 bool IEnumerator.MoveNext() an void IEnumerator.Reset() The help to IEnumerator.MoveNext describes that "after the end of the collection is passed, subsequent calls to MoverNect return false until reset is called" When, however, I have two...
4
8532
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 fine at this point. But when i try to itterate through the collection from within Visual Basic i get the Error: "Object doesn't support this property or method " I know the reason for that is, that i must implement a public function Item.
3
4331
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
3706
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 property "Current". The challange is that I need to implement two versions of the property: one of type System::Object as required by Collections::IEnumerator, the other of type T^ as required by Collections::Generic::IEnumerator<T> ...
11
2220
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 via the GetEnumerator method would be synchronized with the collction; if the collection changes, the existing enumerators are notified via an event. The accompanying EventArgs derived class object carries information about the change to the...
5
9379
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 interface. Please help me resolve the error I was able to implement the non Generics version
3
5769
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 class which derives from IEnumerator? How to implement Current property to return current item of the desired DataTable? Thanks. Emil.
0
9589
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10593
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10329
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9163
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4304
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.