Inconsistency in passing objects from VBA to .NET via COM

Posted by Akash on Stack Overflow See other posts from Stack Overflow or by Akash
Published on 2010-03-29T15:54:14Z Indexed on 2010/03/29 16:43 UTC
Read the original article Hit count: 452

Filed under:
|
|

I have the following interface defined to expose a .NET class to COM:

[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("6A983BCC-5C83-4b30-8400-690763939659")]
[ComVisible(true)]
public interface IComClass
{
    object Value { get; set; }

    object GetValue();

    void SetValue(object value);
}

The implementation of this interface is trivial:

[ClassInterface(ClassInterfaceType.None)]
[Guid("66D0490F-718A-4722-8425-606A6C999B82")]
[ComVisible(true)]
public class ComClass : IComClass
{
    private object _value = 123.456;

    public object Value
    {
        get
        {
            return this._value;
        }

        set
        {
            this._value = value;
        }
    }

    public object GetValue()
    {
        return this._value;
    }

    public void SetValue(object value)
    {
        this._value = value;
    }
}

I have then registered this using RegAsm, and tried to call it from Excel via the following code:

Public Sub ComInterop()

    Dim cc As ComClass
    Set cc = New ComClass

    cc.SetValue (555.555)

    valueByGetter = cc.GetValue 
    valueByProperty = cc.Value 

    cc.Value = 555.555 

End Sub

When I step throught this code, valueByGetter = 555.5555 and valueByProperty = 555.555 as expected. However, I get an "Object required" runtime error on the final line.

Why does setting the value via the setter method work but setting via the property fail? What do I have to change to get the property to work as expected?

© Stack Overflow or respective owner

Related posts about com

Related posts about vba