Hi,

I need to convert some C# code into C++. It starts with:

Dictionary<string, ListViewItem> mItems = new Dictionary<string, ListViewItem>();

I can't find a full example in the C++ Visual Studio documentation.

How do I declare a Dictionary object? A small example would be appreciated.

Thanks.

using namespace System::Collections::Generic;       
//at the top of Form1.h

//here are the details of my little stub just to give you a ready test
//program (I don't know what your specific requirements for the
// LVI are so I just cobbled it into a button event.
ListViewItem ^ lvi = gcnew ListViewItem("test2");
Dictionary<String^,ListViewItem^> myDictionary = 
                                       gcnew Dictionary<String^,ListViewItem^>();
myDictionary.Add("test",lvi);
MessageBox::Show(myDictionary["test"]->Text);

Happy Holidays, hope this helped (I'm really seeing a lot of these C++/CLI specifics for the first time so it's a good experience all around).

Edited 6 Years Ago by jonsca: n/a

Happy Holidays, hope this helped (I'm really seeing a lot of these C++/CLI specifics for the first time so it's a good experience all around).

Yes, it's a good start, thanks!

He's doing the managed code route so his choices are (AFAIK) limited to the .NET spectrum (I think he'd have to marshal in the STL container which I've never done but I've heard nothing good about). I've learned a bit of C# so it looks like stuff I've seen there but with a C++ twist to it.

why not use a std::map? Or did I misread your question?

I'm porting to C++ an existing C# program, so it's easier to stick to the same method.

This article has been dead for over six months. Start a new discussion instead.