none
.NET Collections and COM Interop RRS feed

  • Question

  • Hi there,
    I'm writing a .NET 1.1 assembly which I need to expose via COM so I can access it from Excel VBA.

    I've got some custom collections in my assembly, which I've tried using by inheriting CollectionBase or implementing IEnumerable. Either way I try, I can instantiate the object, but when I try to call the Add method on my collection, I get the following error:

    Run-time error '438':
    Object doesn't support this property or method.

    Which isn't incredibly useful, as it doesn't tell me what's wrong!

    My objects I've exposed via COM work fine, and I can work with them no problem, but when I try to work with these collections, it just doesn't like it.

    If anyone knows what I'm doing wrong, their help would be very much appreciated!

    Thanks in advance,
    James Simm
    Developer,
    ICS Ltd UK.

    jsimm@icsltd-uk.com
    http://www.icsltd-uk.com
    Friday, July 6, 2007 9:08 AM

Answers

  • Generic interfaces won't work in COM anyway.  You can't strongly type objects in the COM world without creating a custom interface.  The only provided collection interface that would work is IList.  You would have to create a custom .NET/COM interface that exposed each IList member using a strongly typed value.  You'd also have to ensure that your Meeting type was exposed to COM. You'd then have to register all the COM interfaces and the class through COM.  You could then have strong type checking (at least pseudo in VB) but you'd be using your IMeetingCollection interface rather than the pre-defined IList interface.

     

    Your .NET code will continue to work the same so there are no problems there.  In the VB code you're always dealing with variants anyway so the type is all but irrelevant.  You can treat the type as Meeting and VB will happily translate it for you or throw a runtime exception.

     

    Michael Taylor - 7/6/07

    http://p3net.mvps.org

     

    Friday, July 6, 2007 2:28 PM
    Moderator

All replies

  • The available members of a COM class are determined by the COM interface you are referencing.  What COM interface are you using when you try to call the Add method?  You should be using IList as this is where Add is defined.  Use QueryInterface (or appropriate VB version) to get access to the interface from the existing COM object.

     

    Michael Taylor - 7/6/07

    http://p3net.mvps.org

     

    Friday, July 6, 2007 1:10 PM
    Moderator
  • If I use IList, I can't strongly type my collection. e.g. My public int Add(Meeting m) would have to be public int Add(object value).

     

    However, thanks for you comments, its given me a couple more ideas on where to look next for a solution!

     

    Thanks,

    James Simm

    Friday, July 6, 2007 1:47 PM
  • Generic interfaces won't work in COM anyway.  You can't strongly type objects in the COM world without creating a custom interface.  The only provided collection interface that would work is IList.  You would have to create a custom .NET/COM interface that exposed each IList member using a strongly typed value.  You'd also have to ensure that your Meeting type was exposed to COM. You'd then have to register all the COM interfaces and the class through COM.  You could then have strong type checking (at least pseudo in VB) but you'd be using your IMeetingCollection interface rather than the pre-defined IList interface.

     

    Your .NET code will continue to work the same so there are no problems there.  In the VB code you're always dealing with variants anyway so the type is all but irrelevant.  You can treat the type as Meeting and VB will happily translate it for you or throw a runtime exception.

     

    Michael Taylor - 7/6/07

    http://p3net.mvps.org

     

    Friday, July 6, 2007 2:28 PM
    Moderator