views:

148

answers:

2

I got the compile error message "Array size cannot be specified in a variable declaration (try initializing with a 'new' expression)" when I tried to declare an array of linked lists.

public LinkedList<LevelNode>[2] ExistingXMLList;

Also, if I wanted to create a small array of strings, isn't the following the correct way?

string [2] inputdata;
+1  A: 

try this:

LinkedList[] ExistingXMLList = new LinkedList[2];
Daniel A. White
Someone up-voted you with a compile error in your answer. :)
ChaosPandion
I thought I was crazy, but then I decided that even with 15.5k rep he could still make a mistake.
Aaron Smith
Programming is hard.
jeffamaphone
Thanks guys!!!!
Daniel A. White
+6  A: 

You declare an array with just [].

LinkedList[] XMLList;

Then you instantiate it with the size.

XMLList = new LinkedList[2];

Or both at the same time:

LinkedList[] XMLList = new LinkedList[2];

To add LinkedLists to this array you type:

XMLList[0] = new LinkedList();
XMLList[1] = new LinkedList();
Aaron Smith
Don't forget to fix your compile error. :)
ChaosPandion
Oops, sorry about that. I never use arrays.
Aaron Smith
No need to apologize, Visual Studio spoils us all!
ChaosPandion
What if you are using an array of a class where the constructor seems to require a paramenter?If you use new like this:XmlTextReader[] r;r = new XmlTextReader [2];Then how would you pass the filename to r[0] and r[1] ?
xarzu
This code is instantiating an Array. If you want to add values to that array you have to do XMLList[0] = new XMLList();
Aaron Smith