Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

Declaring an array of List Generic Class elements

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Eitan - 25 Jun 2007 17:27 GMT
Hello,

I am declaring an element like this:
public static List<myListElement> myList = new List<myListElement>();

I would like to declare an array of this myList.
How would I modify the declaration of myList so it will represents an array?

Thanks
Eitan
colin - 25 Jun 2007 17:40 GMT
> Hello,
>
[quoted text clipped - 7 lines]
> Thanks
> Eitan

public static List<myListElement>[] myList = new List<myListElement>[4];

Colin =^.^=
Eitan - 25 Jun 2007 17:56 GMT
Hello Colin,
Thank you for your answer.  

I tried the line below (before posting my question):
       public static List<myListElement>[] myList = new
List<myListElement>[4]();
The difference is the brackets "()" after "[4]".  The compiler generated an
error.

Can you explain to me why the error, and when would you need the brackets
"()"?

Thank you,
Eitan

> > Hello,
> >
[quoted text clipped - 11 lines]
>
> Colin =^.^=
Tom Spink - 25 Jun 2007 18:05 GMT
> Hello Colin,
> Thank you for your answer.
[quoted text clipped - 26 lines]
>>
>> Colin =^.^=

Hi Eitan,

If you take a look at my response it may be a bit clearer what is going on.
When you're defining the array, you aren't constructing the List<> objects,
just constructing an array of list objects.  Parentheses are not required
for constructing an array, as the construction doesn't take parameters,
only a size of array to create given in the square brackets.

So, that being the case, you'll have to manually construct each List<>
object in your array.

Signature

Tom Spink
University of Edinburgh

Eitan - 25 Jun 2007 19:18 GMT
Tom,

Thank you on both of your answers!

Eitan

> > Hello Colin,
> > Thank you for your answer.
[quoted text clipped - 37 lines]
> So, that being the case, you'll have to manually construct each List<>
> object in your array.
Tom Spink - 25 Jun 2007 17:58 GMT
> Hello,
>
[quoted text clipped - 7 lines]
> Thanks
> Eitan

Hi Eitan,

You can declare it like this:
public static List<myListElement>[] listArray = new List<myListElement>[10];

(To define an array of size 10)

But please note that each element in the array will still have to be
constructed.  You could this in a for-loop:

///
for (int i = 0; i < listArray.Count; i++)
       listArray[i] = new List<myListElement>();
///

Signature

Tom Spink
University of Edinburgh

Eitan - 25 Jun 2007 18:09 GMT
Thank you!

> > Hello,
> >
[quoted text clipped - 22 lines]
>         listArray[i] = new List<myListElement>();
> ///

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.