In a c# assembly, I got a function taking a nullable DateTime as parameter:
public void DoSomething(DateTime? timestamp);
Now I want to call this method from c++/cli:
MyClass->DoSomething(nullptr);
This will not compile. Instead the c++ compiler will print an error message nullptr can not be converted to System::Nullable.
So how do I pass nullptr from c++ to a nullable DateTime?
MyClass->DoSomething(Nullable<DateTime>());
How to use Nullable types in c++/cli?
Nullable
is a value type and C++/CLI doesn’t provide compile-time magic for it. You need to go the explicit route:
System::Nullable<System::DateTime> dtnull;
MyClass->DoSomething(dtnull);
Of course, you can also use a temporary here:
MyClass->DoSomething(System::Nullable<System::DateTime>());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With