Sharon wrote:
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::S tring^,
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::S tring^,
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