C++/CLI DateTime method argument

S

soren.enemaerke

Hi everybody.

I'm writing a .NET wrapper class for a native C++ library and decided
to do it in C++/CLI. One of the problems I ran in to is the following.

In C++ I have the following

public ref class MyClass
{
....
bool DoIt(DateTime^% date);
bool DoIt2([OutAttribute] DateTime^% date)
}

which I think should translate into a method calls with c# syntax:

bool DoIt(ref DateTime date);
bool DoIt2(out DateTime date);

So I was a bit puzzled when the following showed up:

bool DoIt(ref ValueType date);

Where is my type safety? What am I doing wrong? Am I trying to do
something (returning value types from c++/cli via ref and out) that is
just not possible?

bool DoIt(ref ValueType date);
 
B

Ben Voigt [C++ MVP]

Hi everybody.

I'm writing a .NET wrapper class for a native C++ library and decided
to do it in C++/CLI. One of the problems I ran in to is the following.

In C++ I have the following

public ref class MyClass
{
...
bool DoIt(DateTime^% date);
bool DoIt2([OutAttribute] DateTime^% date)
}

which I think should translate into a method calls with c# syntax:

bool DoIt(ref DateTime date);
bool DoIt2(out DateTime date);

So I was a bit puzzled when the following showed up:

bool DoIt(ref ValueType date);

Where is my type safety? What am I doing wrong? Am I trying to do
something (returning value types from c++/cli via ref and out) that is
just not possible?

You're using tracking handles (^) with a value type, which is not needed or
desired.

Just "bool DoIt(DateTime% date);" will work properly.
 
S

soren.enemaerke

Hi everybody.
I'm writing a .NET wrapper class for a native C++ library and decided
to do it in C++/CLI. One of the problems I ran in to is the following.
In C++ I have the following
public ref class MyClass
{
...
bool DoIt(DateTime^% date);
bool DoIt2([OutAttribute] DateTime^% date)
}
which I think should translate into a method calls with c# syntax:
bool DoIt(ref DateTime date);
bool DoIt2(out DateTime date);
So I was a bit puzzled when the following showed up:
bool DoIt(ref ValueType date);
Where is my type safety? What am I doing wrong? Am I trying to do
something (returning value types from c++/cli via ref and out) that is
just not possible?

You're using tracking handles (^) with a value type, which is not needed or
desired.

Just "bool DoIt(DateTime% date);" will work properly.




bool DoIt(ref ValueType date);- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -

Thanks Ben, worked like a charm.
 
S

Sheng Jiang[MVP]

DateTime is a value type, however the track handle ^ makes the parameter to
be boxed. C# does not understand the type of a boxed value type, but C++/CLI
does. So you have ref ValueType in C#, and DateTime^ in C++/CLI.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top