Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 23rd, 2010, 10:04 PM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Default Declaring an array of linked list in C#

Declaring an array of linked list in C#
How do I declare an array of some class in C#? I wonder if it is even allowed or possible.
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.
Code:
public LinkedList<LevelNode>[2] ExistingXMLList;

Also, if I wanted to create a small array of strings, isn't this the way:
Code:
string [2] inputdata;
 
Old April 23rd, 2010, 11:03 PM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

Take a look at the below examples. When you declare, you only declare that a variable is an array of some type, and you can decide its size in the same statement like the first example, or you can simply left the size undecided (untill a later point).

Code:
string[] names = new string[3];
public LinkedList<LevelNode>[] ExistingXMLList;
 
Old April 23rd, 2010, 11:21 PM
Friend of Wrox
 
Join Date: Jun 2005
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Default

What if you are using an array of a class where the constructor seems to require a paramenter?
If you use new like this:
Code:
 
XmlTextReader[] r;
r = new XmlTextReader [2];
Then how would you pass the filename to r[0] and r[1] which would normally be passed in the constructor?
If I was not using an array, it would be done like this:
Code:
 
XmlTextReader r;
r = new XmlTextReader (filename);
 
Old April 23rd, 2010, 11:28 PM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

The following code only "new" the array, but not the individual element of the array:
Code:
XmlTextReader[] r;
r = new XmlTextReader [2];
To "new" an element, you do the following:
Code:
r[0] = new XmlTextReader(filename);





Similar Threads
Thread Thread Starter Forum Replies Last Post
Linked List? pmcizhere Java Basics 2 March 6th, 2008 09:02 PM
C# Object Linked List millsbruce C# 5 July 12th, 2005 04:15 PM
singly linked list Chas508 C++ Programming 1 April 6th, 2005 12:14 AM
declaring public array DorisTan Beginning VB 6 2 January 4th, 2005 09:37 PM
Linked List....!!! amahja56 C++ Programming 4 April 6th, 2004 01:05 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.