4

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.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
user2706838
  • 1,061
  • 2
  • 13
  • 21
  • 3
    Who's throwing the exception? There's obviously a reason you shouldn't be passing null for that argument. – Jonathon Reinhart Jan 02 '14 at 12:03
  • +1. The implementation of the C++ `GetValue()` is most probably throwing that exception because it is illegal to pass null there. (Maybe the error happens a bit down the stack, as the argument name is not one of the arguments of `GetValue()`) – PMF Jan 02 '14 at 12:05
  • Actually i thought that this is common exception and common message. Because C++ throws nothing, and actually - in case it did - how could I catch it in C#? I mean catch with valid message... – user2706838 Jan 02 '14 at 12:09
  • Don't pass null, the library you use doesn't like that when the INI setting is missing. String.Empty is a reasonable alternative. Don't use INI files either, they are a complete perf disaster. – Hans Passant Jan 02 '14 at 12:44

3 Answers3

10

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
    ....
}
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Noted. So... Generally exception is expected behavior? – user2706838 Jan 02 '14 at 12:38
  • 1
    Well, trying to do anything of substance (other than testing for `nullptr`) with a null reference will lead to an exception. What did you do with it? If you want any more help than you already have received, you'd need to show your implementation of `GetValue`. – David Heffernan Jan 02 '14 at 12:54
  • Discovered! Thanks a lot... Some terrible "throw" was found in branch of calling.. – user2706838 Jan 02 '14 at 13:09
  • 1
    @user2706838, it is possibly perfectly fine to pass null for an object. But, as with all APIs, you have to follow the contract. `ArgumentNullException` is exclusively used to tell you that you haven't—which is very helpful, especially when the contract isn't fully documented. – Tom Blodget Jan 03 '14 at 00:16
1

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.

Community
  • 1
  • 1
xaizek
  • 5,098
  • 1
  • 34
  • 60
0

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

string date = MyWrapper.GetValue("SystemSettings", "BuildDate", "");
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335