Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string in C# and String^ in C++/CLI... How to pass "null"?

I got some C++ library. And also I got C++/CLI wrapper for it, to make it possible call methods from this library in C# code.

Lets say I would like to use some call in C# like this:

string date = MyWrapper.GetValue("SystemSettings", "BuildDate", null);

which will call next function on C++/CLI:

static String^ GetValue(String^ section, String^ key, String^ defaultValue)

My problem: I got next ArgumentNullException:

Value cannot be null.\r\nParameter name: managedString.

So... Question: how should I pass null correctly? Thanks.

like image 710
user2706838 Avatar asked Dec 19 '22 19:12

user2706838


1 Answers

Your code that passes null is fine. The problem is that your wrapper code needs to detect null and deal with it. That code is presumably written under the assumption that the third parameter is never null. If you wish to allow null, you must explicitly handle that condition:

static String^ GetValue(String^ section, String^ key, String^ defaultValue)
{
    if (defaultValue == nullptr)
        // special case handling
    ....
}
like image 87
David Heffernan Avatar answered Dec 22 '22 08:12

David Heffernan