Creating KeyValuePair in Managed C ++

I have another C ++ managed question KeyValuePair where I know what to do in C #, but it's hard for me to switch to managed C ++. Here is the code that does what I want to do in C #:

KeyValuePair<String, String> KVP = new KeyValuePair<string, string>("this", "that"); 

I reflected this in MC ++ and get the following:

 KeyValuePair<String __gc*, String __gc*> __gc* KVP = (S"this", S"that"); 

which I translated into:

 KeyValuePair<String ^, String ^> KVP = (gcnew String("this"), gcnew String("that")); 

I know from my previous question that KeyValuePair is a value type; The problem is that it is a value type in C ++ and a reference type in C #? Can someone tell me how to set KeyValuePair key and value from C ++?

+4
source share
2 answers

This should do it:

KeyValuePair< String ^, String ^> k(gcnew String("Foo"), gcnew String("Bar"));

KeyValuePair is an immutable type, so you need to pass everything to a constructor that looks the same as in C #, except that you write it like this if the object is on the stack.

+3
source

to try

 System::Collections::Generic::KeyValuePair< System::String^, System::String^>^ k = gcnew System::Collections::Generic::KeyValuePair< System::String^, System::String^>(gcnew System::String("foo") ,gcnew System::String("bar")) ; 
+1
source

All Articles