Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

C# class indexer access from VB6 via COM interop

$
0
0

Hello,

I'm operating in a scenario where legacy VB6 code must interoperate with C# code. So far no problems except one special case where a C# library must substitude a former ActiveX-DLL.

The problem that arises is the indexer of a C# class that is being consumed from VB6. I can get values using the indexer without issues but when trying to set values from VB6 I'm running into error 424 "Object required".

Here's the C# code:

[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]  
[ComVisible(true)]    
public interface ITestList 
{         [DispId(0)]      Object this[short Index] 
     {             [DispId(0)]set;           [DispId(0)]get;          
     }        
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class TestList : ITestList
{
    private List<Object> internalList = new List<Object>();

    public TestList()
    {
    }        

    public Object this[Object Index]
    {
        [return: MarshalAs(UnmanagedType.Struct)]
        get
        {
            // code for setter goes here
            return "test";                   
        }
        [param: MarshalAs(UnmanagedType.Struct)]
        set
        {
            // code for setter goes here but even without code the error occurs
        }
    }

}

Access from VB6 code:

'This is working
MsgBox obj.Item(1)
'This throws 424 "object required"
obj.Item(1) = "test me"

I've already investigated a lot but none of the findings solved the final problem. Please note that I'm not allowed to change the way VB6 is accessing the property. Thus the workaround to implement custom methods cannot be used.

I'm sure there's a way to implement the indexer in a way it's setter can be accessed from VB6 or at least I'd want to understand why this won't work.

Regards



Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>