C # call from C ++: how to pass nullptr to DateTime?

In C # assembly, I got a function with the DateTime parameter as nullable:

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 prints an error message. Nullptr cannot be converted to System :: Nullable.

So how do I pass nullptr from C ++ to nullable DateTime?

+5
source share
2 answers
MyClass->DoSomething(Nullable<DateTime>());

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

+10
source

Nullable- the type of value, but C ++ / CLI does not provide compile time for it. You need to go to the explicit route:

System::Nullable<System::DateTime> dtnull;
MyClass->DoSomething(dtnull);

Of course, you can also use temporary here:

MyClass->DoSomething(System::Nullable<System::DateTime>());
+4
source

All Articles