Using a C# Collection through Interop with VB6

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
The Problem is that the IList interface forces me to implement "public object
this[int index]" and now i cannot publicate another function "Item" with the
same functionality. AFAIK Visual Basic need the NewEnum function and the Item
function to work properly.

May anyone of you be so kind and help me, i'd be deeply grateful ??
 
Hello,

Could you please elaborate on how are you trying to iterate through the
collection in VB6?
If you're using ForEach, you'll need to implement the _NewEnum property and
a kind of IEnumerator interface - it's COM counterpart to be more exact.
 
Hi,

exactly I'm trying to use for each in VB6. My Class is already derived from
IEnumerable but i quess this is only helpfull for iterating with .NET.

Can you try to explain me how i can write my own [_NewEnum] function in C#.
I don't even find the IUnknown Interface that this Function must return.

thx for your effort


Dmitriy Lapshin said:
Hello,

Could you please elaborate on how are you trying to iterate through the
collection in VB6?
If you're using ForEach, you'll need to implement the _NewEnum property and
a kind of IEnumerator interface - it's COM counterpart to be more exact.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Benjamin Piorczig said:
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.
The Problem is that the IList interface forces me to implement "public
object
this[int index]" and now i cannot publicate another function "Item" with
the
same functionality. AFAIK Visual Basic need the NewEnum function and the
Item
function to work properly.

May anyone of you be so kind and help me, i'd be deeply grateful ??
 
Benjamin Piorczig said:
Hi,

exactly I'm trying to use for each in VB6. My Class is already derived
from
IEnumerable but i quess this is only helpfull for iterating with .NET.

Can you try to explain me how i can write my own [_NewEnum] function in
C#.
I don't even find the IUnknown Interface that this Function must return.

thx for your effort

You need to explicitly implement IEnumerable AND add a GetEnumerator()
member method marked with DispIdAttribute to give it a DISPID -4.
That way the IEnumerator interface is exported to COM as IEnumVARIANT.


public class MyEnumerableObj : IEnumerable
{
Hashtable myTable;
....

// GetEnumerator explicit implementation

IEnumerator IEnumerable.GetEnumerator()
{
return myTable.Values.GetEnumerator();
}

// COM friendly strong typed GetEnumerator

[DispId(-4)]
public IDictionaryEnumerator GetEnumerator()
{
myTable.GetEnumerator();
}

Willy.
 
Thanks very much Willy,

I can now iterate through the items with foreach from within vb6 and vbscript.
 
Back
Top