-1

I want to map some C++ to C# via CLI/C++. In C++ I have a vector<map<string,string>> which I thought I could represent in .Net as List<Dictionary<String,String>. However, this gives me errors.

List<Dictionary<String,String>> // is not a valid generic argument

Is there a standard alternative?

Note I'm implementing this in CLI/C++.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Pat Mustard
  • 1,852
  • 9
  • 31
  • 58
  • Does anything in here help? Not too familiar with C++, but while a vector and map may behave like a List and Dictionary in C#, they are different types which is important when doing interop. http://stackoverflow.com/questions/31417688/passing-a-vector-array-from-unmanaged-c-to-c-sharp – Michael Stum Aug 19 '16 at 05:45
  • "gives me errors", please post the actual error messages you're getting and the whole code, there should be no error message from the code you've posted. Also, if the error is that you're trying to figure out what to call a C++ method with, from C#, can you please tell us what intellisense says about the whole thing? I think it should tell you what the types of things should be even in this scenario. – Lasse V. Karlsen Aug 19 '16 at 06:52
  • The error is there. Not a valid generic arg... – Pat Mustard Aug 19 '16 at 07:22
  • The snippet is entirely too short to see the problem, but one obvious mistake (if this is C++/CLI code) is that you forgot to use the ^ hat. Required on reference types. So it is `List^>^ arg`. Converting to the unmanaged collection types is something you'll have to do yourself, one element at a time. A painful and slow affair. – Hans Passant Aug 19 '16 at 07:27
  • I'm afc now but I'm pretty sure I tried that. Will confirm when I get back. Re the conversions - yup... Pretty tedious :) – Pat Mustard Aug 19 '16 at 07:35

2 Answers2

1

You missed one > This may be a Typo, Any way the declaration should be like this:

List<Dictionary<String,String>> DictList = new List<Dictionary<String,String>>();

You can initialize then like this:

List<Dictionary<String, String>> DictList = new List<Dictionary<String, String>>()
                                        {
                                            new Dictionary<String,String>()
                                            {
                                                {"Key 1","value 1" },
                                                {"Key 2","value 2" }
                                            },
                                            new Dictionary<String,String>()
                                            {
                                                {"Key 1","value 1" },
                                                {"Key 2","value 2" }
                                            }
                                        };
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

It turns out that I need to pass handles to generic containers:

List<Dictionary<String^, String^>^>^ list_of_dict_of_string;

Thanks, all.

Pat Mustard
  • 1,852
  • 9
  • 31
  • 58