Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call c# from c++: how to pass nullptr to DateTime?

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?

like image 927
Sam Avatar asked Mar 31 '11 14:03

Sam


2 Answers

MyClass->DoSomething(Nullable<DateTime>());

How to use Nullable types in c++/cli?

like image 167
Daniel A. White Avatar answered Oct 04 '22 13:10

Daniel A. White


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>());
like image 28
Konrad Rudolph Avatar answered Oct 04 '22 12:10

Konrad Rudolph