tags:

views:

66

answers:

4

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?

Edit: I've had some useful responses, so my additional question is "will this issue arise with COM clients written in other languages, or is it specific to VBA?".

A: 

You need to use the Set keyword on the last line :

Set cc.Value = 555.555
Thibault Falise
When I try that it fails with a compile error: object required.
Akash
A: 

have you tried something like (vb a bit rusty, you get the idea):

Dim newValue as Int
int= 555.555
cc.Value = newValue (or maybe set cc.Value=newValue)
Sam Holder
Sorry, doesn't help - same errors as the original :(
Akash
+1  A: 

Your interface is exported to the type library like this:

dispinterface IComClass {
    properties:
    methods:
        [id(00000000), propget]
        VARIANT Value();
        [id(00000000), propputref]             // <=== problem here
        void Value([in] VARIANT rhs);
        [id(0x60020002)]
        VARIANT GetValue();
        [id(0x60020003)]
        void SetValue([in] VARIANT Value);
};

The trouble is the Value property setter, it is declared as propputref instead of propput. That there's a difference at all is a nasty consistency issue in COM, brought about by it supporting a default property. That's why you have to use the Set keyword in VBA. Problem is: you're not passing an object in the VBA code, even though .NET expects one.

After researching this a bit, I found no ready solution for this problem. There's a small chance that the Let keyword might work in VBA, I didn't try it. The only halfway decent fix is to force late binding, instead of ComInterfaceType.InterfaceIsDual use ComInterfaceType.InterfaceIsIDispatch. Or avoid using "object".

Hans Passant
Thanks for the info - not the answer I wanted though :) So is this problem only going to occur in a VBA client? I've been testing my COM interface through VBA but have no control over the language that the clients will be written in.
Akash
Do you *really* have to support a Variant? Do you *really* have to support early binding? There is not enough info in your question to make that call.
Hans Passant
I feel that early binding + intellisense makes it significantly easier for consumers of my API (and hence fewer support calls!). As for supporting variant (object), given the types that I need Value to accept I could specify that Value is a string and interpret it appropriately, but that will seem strange to .NET clients (maybe no stranger than it being object...).In any case, do you think that the original problem is specific to VBA or a more general COM issue?
Akash
+1  A: 

This seems to be an issue with tlbexp and MS advices using late-bound calls like this:

Dim cc As Object ' this one changed from ComClass
Set cc = New ComClass
...
cc.Value = 555.555

You can leave it early-bound and try a much simpler fix:

Set cc.Value = CVar(555.555)
wqw
You late-bound suggestion works, but the CVar approach gives the same errors as before (I've tried both with and without the Set keyword).
Akash
Do you know whether I will encounter the same problem from a client written in a different language? i.e. is this a COM problem or a VBA problem?
Akash
This is .Net COM interop problem. Are you sure `CVar` does not change the error message?
wqw
I checked the CVar approach again and it definitely fails :( Given that this is a .NET COM interop problem and that other clients may not have the late-binding option, it sounds like I need to either replace the property with getter/setter or else change the property type.
Akash
Both this answer and the one from nobugz have been very helpful. His answer gave a bit more background into the cause of the problem, so I've set his as the accepted answer - hope that is ok.
Akash