3

using C++/CLI I want to declare a list of a list.
When declaring a regular list, I write:

List<String^>^ NameOfList = gcnew List<String^>(2);

Consequently, I tried to declare the list of a list like:

List<List<String^>^>^ AnotherName = gcnew List<List<String^>(2)>(2);

However, Microsoft Visual Studio complains about the argument on the right hand side saying it is not valid.

Note, I can create an empty list with

List<List<String^>^>^ AnotherName;

which works fine. Does anybody know what is wrong here?

1 Answers1

6

You are playing fast and loose with the constructor argument, it is not part of the type signature. It sets the initial capacity of the list, there are not an enormous number of cases where 2 makes a lot of sense. Probably best to not use it all until you grokked what the Capacity property does.

Best guess for the kind of code you are looking for:

int size = 2;
List<List<String^>^>^ AnotherName = gcnew List<List<String^>^>(size);
for (int ix = 0; ix < size; ++ix) AnotherName->Add(gcnew List <String^>);
Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536