473,385 Members | 1,396 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,385 software developers and data experts.

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

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.
Nov 15 '05 #1
1 3840
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 [.NET/C# MVP]" <ni**************@exisconsulting.com> wrote
in message news:%2***************@TK2MSFTNGP12.phx.gbl...
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]
- ni**************@exisconsulting.com

"Aaron Queenan" <aq*********************@contingent.com.au> wrote in message news:Oy**************@TK2MSFTNGP12.phx.gbl...
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.


Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jon Slaughter | last post by:
Lets assume I want to write a whole host of classes having "property" elements in them and create a bunch of objects from the different classes. Lets suppose that they are circles, lines, etc......
24
by: downwitch | last post by:
Hi, I know this has been covered here and in the .public groups, but it seems like it's been a while, especially around here, so I just thought I'd ask again to see if anyone has figured out a...
0
by: Peter Verburgh | last post by:
Hello, I'm using PropertyGrid control to show the properties of an control. There are standard properties for each usercontrol like "Cursor, Dock ,...ect" I don't want to show this properties...
15
by: Gary Morris | last post by:
Hello all, OK, first of all I have known about properties since VB6, which I have and have used extensively. It seems that property get and set are basically the same concept in C# and VB.NET,...
1
by: lester | last post by:
I'm using a C++ dll and need to allow unsafe code because the dll function takes char *. I am using Visual Studio.NET 2002. Everything I've seen says to go to the project properties page, then the...
8
by: Chuck Bowling | last post by:
Is there any justification - from an OOP perspective - for wrapping an attribute in a Property beyond the ability to restrict access? private int myInt; public int MyInt { get { return...
45
by: Brett | last post by:
If I do this without declaring a corresponding field, is it considered bad design? What are the advantages or disadvantages to either method? Notice there is not set. public string URL { get...
2
by: Özden Irmak | last post by:
Hello, Is there an easy way to save some/all properties of a windows forms control into a type of file? I came acros with "Serializable" attribute but it seems that I have to inherit evey...
8
by: Joel Reinford | last post by:
I would like to build a class that has properties which can be accessed by string names or index numbers in the form of MyClass.Item("LastName"). The string names or item index values would be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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...

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.