|
-
Oct 27th, 2012, 05:53 AM
#1
Using Lists
(Newbe Question)
I'm playing around with Lists
I can do:
Code:
Dim myList(1) As List(Of String)
I can't do:
Code:
Dim myList(1) As New List(Of String)
the IDE returns "Arrays cannot be declared with 'New'"
I circumvent the 'problem' by putting
Code:
myList(0) = New List(Of String)
myList(1) = New List(Of String)
in the Form Load event.
The question is, is there another way to instantiate the List array?
-
Oct 27th, 2012, 05:58 AM
#2
Re: Using Lists
The line
vb.net Code:
Dim mylist() As List(Of String) = {New List(Of String), New List(Of String)}
will do the equivalent of what you expected to happen with Dim mylist(1) As New List(Of String).
#EDIT: Alternatively you could use linq to create an array of <count> new lists of string with:
vb.net Code:
Dim mylist() As List(Of String) = Enumerable.Range(0, <count>).Select(Function(i) New List(Of String)).ToArray
Last edited by ThomasJohnsen; Oct 27th, 2012 at 06:03 AM.
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Oct 27th, 2012, 06:06 AM
#3
Re: Using Lists
Thanks. I can see that's ok for 1 or 2 elements, but if I had, say 100, would I just resort to my 'circumvention' method i.e.
Code:
For I = 0 To 99
myList(I) = New List(Of String)
Next
or is there yet another way ?
EDIT: Ok, thanks again. Just read your Edit.
-
Oct 27th, 2012, 06:28 AM
#4
Re: Using Lists
Just fyi there was a member here, that investigated the performance of some linq. In most (all he had examined iirc) cases the naive (or old-fashioned) method of doing things with loops or similar will be faster or at least as fast as linq. The thing promoting linq is ofc the ability to make short and easy to read/understand code very fast.
In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)
-
Oct 27th, 2012, 11:16 AM
#5
Re: Using Lists
This question has nothing specific to do with Lists. It's the same for any array of reference types, and kinda for value types too. Just a variable is equal to Nothing when you declare it, so each element of an array is Nothing when you create the array. In the case of value types, Nothing is equivalent to the default value for that type, e.g. 0 for Integers, False for Booleans and #1/01/0001# for Dates. If you want to assign a value other than the default to each element of the array then you must do that explicitly. The simplest way to do that for any size array is with a loop:
Code:
Dim lists(upperBound) As List(Of String)
For i = 0 To upperBound
lists(i) = New List(Of String)
Next
Alternatively, you could use LINQ to create the array in the first place:
Code:
Dim lists = Enumerable.Range(1, length).
Select(Function(n) New List(Of String)).
ToArray()
-
Oct 27th, 2012, 11:34 AM
#6
Re: Using Lists
I'm curious. Why an Array of Lists rather than a List of Lists?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Oct 27th, 2012, 11:45 AM
#7
Re: Using Lists
 Originally Posted by dunfiddlin
I'm curious. Why an Array of Lists rather than a List of Lists?
One would assume that the OP wants a fixed number of lists with a variable number of items rather than a variable number of lists with a fixed number of items. Of course, assumption is a good way to get yourself in trouble around here.
-
Oct 27th, 2012, 12:58 PM
#8
Re: Using Lists
OP is trying to get to grips with VB.Net and 'needed' something like an array of Collections. I was looking at this thread http://www.vbforums.com/showthread.p...85#post4266985 and was trying to work out how I'd store the Questions and Answers. Came to the conclusion that a List for each subject would work. (as would a UDT Array, nD Array and many other things that I haven't discovered yet no doubt) but as I hadn't looked at Lists before thought I'd give it a try.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|