Inconsistency in passing objects from VBA to .NET through COM

I have the following interface designed to display the .NET class for 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;
    }
}

Then I registered this with 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 go through this code, the value is ByGetter = 555.5555 and valueByProperty = 555.555, as expected. However, I get the "Required Object" error on the final line.

Why does setting the value using the setter method work, but setting using the property completes? What do I need to change to make the property work as expected?

: , : " COM-, , VBA?".

+3
4

:

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);
};

Value, propputref propput. , , - COM, , . Set VBA. , VBA, .NET .

, . , Let VBA, . - , ComInterfaceType.InterfaceIsDual ComInterfaceType.InterfaceIsIDispatch. "".

+4

, tlbexp MS, :

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

:

Set cc.Value = CVar(555.555)
+1

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

Set cc.Value = 555.555
0
source

You tried something like (vb is a little rusty, you get it):

Dim newValue as Int
int= 555.555
cc.Value = newValue (or maybe set cc.Value=newValue)
0
source

Source: https://habr.com/ru/post/1738997/


All Articles