Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using a C# Collection through Interop with VB6

126 views
Skip to first unread message

Piorczig@discussions.microsoft.com Benjamin Piorczig

unread,
Feb 11, 2005, 4:49:04 AM2/11/05
to
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 ??

Dmitriy Lapshin [C# / .NET MVP]

unread,
Feb 11, 2005, 7:24:57 AM2/11/05
to
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" <Benjamin Pior...@discussions.microsoft.com> wrote in
message news:BB9F208B-6279-4552...@microsoft.com...

Benjamin Piorczig

unread,
Feb 11, 2005, 7:45:02 AM2/11/05
to
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

Willy Denoyette [MVP]

unread,
Feb 11, 2005, 9:35:37 AM2/11/05
to

"Benjamin Piorczig" <Benjamin...@discussions.microsoft.com> wrote in
message news:6B18D5F2-5A0B-45EB...@microsoft.com...

> 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.


Benjamin Piorczig

unread,
Feb 14, 2005, 2:27:01 AM2/14/05
to
Thanks very much Willy,

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

"Willy Denoyette [MVP]" wrote:

> 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;

> ....

0 new messages