user2706838
user2706838

Reputation: 1171

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.

Upvotes: 4

Views: 5567

Answers (3)

xaizek
xaizek

Reputation: 5252

There is a similar limitation for Windows Runtime. See this verbose answer to a similar question. Looks like you might have trapped on something very close to it.

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311048

Maybe you need to pass an empty string instead of null. For example

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

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 613192

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
    ....
}

Upvotes: 10

Related Questions