Reputation: 457
Is there a way to create a Dictionary of Dictionaries in C++ CLI? I was previously using a Dictionary of String,ArrayList pairs and found it useful to have my ArrayList converted to a Dictionary of String,CustomValueStruct pairs instead to make use of the key matching.
The code I had for the Dictionary of String,ArrayList pairs:
Dictionary<String^, ArrayList^> myDictionary = gcnew Dictionary<String^, ArrayList^>();
How can I convert this to use a Dictionary of Dictionaries?
The code I tried using, which doesn't compile is:
Dictionary<String^, Dictionary<String^, CustomValueStruct>> myDictionary = gcnew Dictionary<String^, Dictionary<String^, CustomValueStruct>>();
Where CustomValueStruct
is just a custom struct in my code, which previously lived in the ArrayList.
The result of the attempted line is:
error C3225: generic type argument for 'TValue' cannot be 'System::Collections::Generic::Dictionary<TKey,TValue>', it must be a value type or a handle to a reference type
Upvotes: 0
Views: 5590
Reputation: 10875
As the compiler said, you forgot a ^
:
auto myDictionary = gcnew Dictionary<String^, Dictionary<String^, CustomValueStruct>^>();
Note that you need create a Dictionary<String^, CustomValueStruct>
when adding items to myDictionary
:
myDictionary.Add("Key", gcnew Dictionary<String^, CustomValueStruct>());
Finally, be sure you really want CustomValueStruct
to be a value type; you're usually better off writing
ref class CustomRefClass { /* ... */ };
which in turn requires a ^
for the nested Dictionary
:
auto myDictionary = gcnew Dictionary<String^, Dictionary<String^, CustomRefClass^>^>();
Upvotes: 1