Results 1 to 8 of 8

Thread: Using Lists

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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?

  2. #2
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: Using Lists

    The line
    vb.net Code:
    1. 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:
    1. 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)

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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.

  4. #4
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    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)

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Using Lists

    Quote Originally Posted by dunfiddlin View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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
  •  



Click Here to Expand Forum to Full Width