How should DISPID_VALUE properties be put in the tlb for (easy) use by C#

A

Aaron Queenan

I have the following in the idl for a COM collection:

[propget, id(DISPID_VALUE), helpstring("property Item")] HRESULT
Item([in] VARIANT* Index, [out, retval] LPDISPATCH* punk);

This allows VBA to access objects in the collection using the syntax
collection("key") or collection(1).

When I import this type library into C#, the DISPID_VALUE property appears
as 'this[]', and when I try to use the syntax similar to VBA, the compiler
complains that I must use collection.get_Item() instead of collection().

1. How should I form the idl so that I can use the collection.Item[]
syntax, as I would be able to if I didn't use DISPID_VALUE?

2. If I use the collection.get_Item() syntax, I must convert the Index
parameter to an 'object ref', and the method returns an object. How should
I change the idl so that the method accepts a 'string' and returns the
correct object type?

Thanks,
Aaron Queenan.
 
A

Aaron Queenan

I've just changed:

[propget, id(DISPID_VALUE), helpstring("property Item")]
HRESULT Item([in] VARIANT* Index, [out, retval] LPDISPATCH* punk);

to:

[propget, id(DISPID_VALUE), helpstring("property Item")]
HRESULT Item([in] VARIANT Index, [out, retval] IChain** ppChain);

Thanks for your suggestion. Changing VARIANT* to VARIANT fixes the
problem - i.e. the [] syntax works correctly. C# doesn't seem to cope with
the [] syntax when passing the index by reference, which could be a problem
with other collections. :-(

Next problem, is that I can now access the objects using collection["key"],
but if I use collection[1] I get a stack overflow. It works fine in VBA.

Thanks,
Aaron Queenan.

Nicholas Paldino said:
Aaron,

You should be able to use the square brackets to access the information.
However, you will still have to pass it as an object reference.

You could change the IDL so that it takes a variant by value (remove the
pointer), but then you would have to change the underlying implementation as
well. If you change it so that it takes a string (change Variant to BSTR)
then you will not have the ability to access the items by index.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have the following in the idl for a COM collection:

[propget, id(DISPID_VALUE), helpstring("property Item")] HRESULT
Item([in] VARIANT* Index, [out, retval] LPDISPATCH* punk);

This allows VBA to access objects in the collection using the syntax
collection("key") or collection(1).

When I import this type library into C#, the DISPID_VALUE property appears
as 'this[]', and when I try to use the syntax similar to VBA, the compiler
complains that I must use collection.get_Item() instead of collection().

1. How should I form the idl so that I can use the collection.Item[]
syntax, as I would be able to if I didn't use DISPID_VALUE?

2. If I use the collection.get_Item() syntax, I must convert the Index
parameter to an 'object ref', and the method returns an object. How should
I change the idl so that the method accepts a 'string' and returns the
correct object type?

Thanks,
Aaron Queenan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top