FINAL EDIT
I've more thoroughly tested the "feature" and written a complete article about it: Optional passed by reference parameters with C# for VBA COM APIs
Any feedback is welcome.
For whatever reason in C# a method can't have optional passed by reference (marked with ref
or out
) parameters.
But in other infrastructures like VBA, VB6 or COM this is possible.
When bridging C# with VBA using COM I've found a way to overcome the C# limitation by marking the ref
parameters as [Optional]
which is AFAIK what the C# compiler generates at the end of its plumbing.
void SomeDotNetCSMethod([Optional] ref someParameter)
This seems to work like a charm: when calling from VBA to C# I can omit the ref
parameters and of course I can read and change them from the .Net/C# side.
But as I've not found any writing about it I'd like to be sure there is no bad side effects with this "trick".
Do you know any ? Can you imagine any ?
EDIT
With [DefaultParameterValue]
attribute I may have found a way to have VBA see the default values for the parameter, but only for some types.
I'll do more tests to confirm this is working as expected...
SomeDotNetCSMethod_1
,SomeDotNetCSMethod_2
...