Type mismatch error causing C # dll from VBA using com interop

What are the restrictions for parameters when calling C # methods from VBA code using COM interaction?

I find that if I call a method that accepts one simple type (string, int, etc.), it works, but it calls a method that calls a non-standard class as a parameter, with a "Mismatch Type" error when compiling VBA code .

C # code:

public namespace foo {
    public class XXX { }

    public class Service {
        public setString(String val) { ... }
        public setXXX(XXX val) { ... }
    }
}

VBA Code:

Dim service As New foo.Service
service.setString("this works")

Dim complexClass as New foo.XXX 

Rem This fails with a type mismatch error
service.setXXX(complexClass)

As mentioned above, the VBA compiler pinches this line: service.setXXX (complexClass)

Any thoughts?

+3
source share
1 answer

setString setXXX , , void - . setXXX void, () VBAs, :

service.setXXX complexClass

:

VBA sub-rot . , :

//C#
public void setXXX2(XXX val, XXX val2) { }

.

'VB
service.setXXX2 (complexClass, complexClass) 'Error

, , Parenthesized Expression, - String .

, , GetParameterType, :

public class Service { 
    public void setString(String val) {  }
    public void setXXX(XXX val) { }
    public void setXXX2(XXX val, XXX val2) { }

    public string GetParameterType(object val) {
        return val.GetType().Name;
    }
} 

, , , .

MsgBox service.GetParameterType(complexClass) ' Returns XXX
MsgBox service.GetParameterType((complexClass)) ' Returns String
+1

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


All Articles