Search code examples
c++-climanaged

C++/CLI: KeyValuePair<> Key and Value don't exist


I'm writing my first class library using C++/CLI and I've encountered an error when trying to access Key and Value properties of KeyValuePair class.

KeyValuePair<String^, LONG>^ params = gcnew KeyValuePair<String^, LONG>(readerName, hContext);

When I make a breakpoint on it and check using Visual Studio 2010, what's in params variables, it correctly shows two private variables that hold key and value, but for Key and Value properties it shows an error saying something like "Key does not exist".

ошибка: "System::Collections::Generic::KeyValuePair<System.String ^,int>(params.Key" не существует

Solution

  • LONG is probably not the best thing to use as an argument type for any generic class. It may or may not even be defined depending on how you are building. Use int or long, or better yet, Int32 or Int64 instead.

    The latest versions of Visual Studio don't support Intellisense for C++/CLI and often watch variables cannot be deciphered at all, let alone partially as you're seeing.

    Bottom line: Don't expect the IDE/Debugger to work well at all for C++/CLI code. Just output to a debug stream or otherwise test the variable in your own code to see if it looks/works as expected. Don't trust the debugger when it comes to Managed C++.