VS2017 Error Highlighting issue when converting C# bool? to C++/CLI bool


I have a VS2017 unmanaged C++ dll project with a single file set to be managed C++/CLI.

I did this for two reasons:

  1. Because the dll has to call functionality from a C# dll API.

  2. My Dll has to make C functions available via LoadLibrary calls to
    meet a standard.

The issue I am having is this:

In the C# dll API there is a bool? property. In my C++/CLI file I need to evaluate it as a bool.

I tried the following:

if (!(bool)textMessages[i]->IsDirectionToVehicle)

(where IsDirectionToVehicle is the bool? I am trying to evaluate)

This builds fine, but the VS Error Highlighter (“Intellisense” i think) marked this as an error with the message:

“no suitable conversion function from “System::Nullable” to “bool” exists.”

And when I hover over IsDirectionToVehicle the tooltip indicates it’s a System::Nullable (not System::Nullable<Boolean> which I think is the object class for bool?).

Q: Is this just an issue with error highlighting, or is it something I need to investigate more?

Q: In C# I would have done something like:

if (property.HasValue ? property.Value : false)

But since VS sees this property as just Nullable, it has no HasValue method. Yet it compiles, so does the implementation of C++/CLI do this conversion in the background?


Please enter your comment!
Please enter your name here