How to ? out argument in managed c++

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello gurus,

I have the following C# function signature:
void TheFunction(Dictionary<string, MyClassObj> dicName, out int nOne, out
int nTwo, out int nThree, out int nFour);

And it works fine.

I'm trying to write a managed C++ function that will save the save
signature, but till now I've got all kind of compiler error.

Can anyone tell me how a managed C++ function signature should look like
that will do the job like the one I showed above?
 
Sharon said:
Hello gurus,

I have the following C# function signature:
void TheFunction(Dictionary<string, MyClassObj> dicName, out int nOne, out
int nTwo, out int nThree, out int nFour);

And it works fine.

I'm trying to write a managed C++ function that will save the save
signature, but till now I've got all kind of compiler error.

Can anyone tell me how a managed C++ function signature should look like
that will do the job like the one I showed above?

For VC++ 2005 (C++/CLI):

void TheFunction( System::Collections::Generic::Dictionary<System::String^,
MyClassObj^>^ dicName, [System::RuntimeServices::Interop::Out] int% nOne,
[System::RuntimeServices::Interop::Out] int% nTwo,
[System::RuntimeServices::Interop::Out] int% nThree,
[System::RuntimeServices::Interop::Out] int% nFour);

For VC++ 2002/2003 (Managed Extensions for C++):

Buggy, won't be fixed. Upgrade to VC++ 2005. Don't complain about price,
Express edition is free.
 
Thanks Ben,

I 'm using the Framework 2. Sorry for not pointing that our earlier.

Your solution works fine.
 
Hi Ben,

The function signature still works but I have strange problem checking if
the dicName is null or not...

In the function (VC++ 2005 (C++/CLI)):

void TheFunction( System::Collections::Generic::Dictionary<System::String^,
MyClassObj^>^ dicName, [System::RuntimeServices::Interop::Out] int% nOne,
[System::RuntimeServices::Interop::Out] int% nTwo,
[System::RuntimeServices::Interop::Out] int% nThree,
[System::RuntimeServices::Interop::Out] int% nFour)
{
if( dicName != NULL ) // Error
{
if( dicName ["key"] != NULL ) // Error
{
...
}
...
}
}

I tried some other ways to check it, but I'm still getting all kind of
compiler error.

How do I check if it's null or not if the above two cases?
 
Sharon said:
Hi Ben,

The function signature still works but I have strange problem
checking if the dicName is null or not...

In the function (VC++ 2005 (C++/CLI)):

void TheFunction(
System::Collections::Generic::Dictionary<System::String^,
MyClassObj^>^ dicName, [System::RuntimeServices::Interop::Out] int%
nOne, [System::RuntimeServices::Interop::Out] int% nTwo,
[System::RuntimeServices::Interop::Out] int% nThree,
[System::RuntimeServices::Interop::Out] int% nFour) {
if( dicName != NULL ) // Error
{
if( dicName ["key"] != NULL ) // Error
{
...
}
...
}
}

I tried some other ways to check it, but I'm still getting all kind of
compiler error.

How do I check if it's null or not if the above two cases?

NULL is a macro #defined to be 0. As you've discovered, you can't compare a
managed reference to 0. Instead, C++/CLI adds a new named constant,
nullptr, for just this purpose. You can also use nullptr in unmanaged code,
and it's expected to be an official part of the next C++ standard ("C++
09").

Also, a point of usage of Dictionary<TKey,TVal>: To check for the presense
of an item, use the ContainsKey member function, rather than using the
indexer (operator []). Using the indexer will actuall add the key to the
dictionary and associate it with a null object reference. So:

void TheFunction( System::Collections::Generic::Dictionary<System::String^,
MyClassObj^>^ dicName, [System::RuntimeServices::Interop::Out] int% nOne,
[System::RuntimeServices::Interop::Out] int% nTwo,
[System::RuntimeServices::Interop::Out] int% nThree,
[System::RuntimeServices::Interop::Out] int% nFour)
{
if( dicName != nullptr)
{
if( dicName->ContainsKey("key"))
{
...
}
...
}
}

-cd
 
Back
Top