none
List in a list? RRS feed

  • Question

  • Getting error on this its a syntax error:

    Dim ItemList As New List(Of String(Of String))
    
        Private Sub frmBlack_Market_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ItemList.Add("Item", "90")
    
        End Sub

    not sure how to make this work

    Error 9 Array bounds cannot appear in type specifiers. C:\Users\Holmes\Documents\Visual Studio 2010\Projects\Space Game\Space Game\Space Game\Menus\frmBlack_Market.vb 5 40 Space Game

    Here: Dim ItemList As New List(Of String(Of String))

    Friday, September 20, 2013 2:06 PM

Answers

  • I think what you're trying to do is this:

    Dim myList As New List(Of List(Of String))


    Please call me Frank :)

    Friday, September 20, 2013 2:32 PM
  • Hi,

    Do you really want a LIST of Lists?

    Dim list1 as New List(Of String)

    Dim list2 As New List(Of String)

    Dim listOfLists As New List(Of List)

    would probably work.



    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

    Friday, September 20, 2013 2:12 PM
  • Hi

    Here is my take on what you want:

            Dim ItemList As New List(Of List(Of String))
    
            Dim Items1 As New List(Of String)
            Dim Items2 As New List(Of String)
            Dim Items3 As New List(Of String)
            Items1.AddRange({"One", "Two", "Three"})
            Items2.AddRange({"Fred", "Brian", "Mary"})
            Items3.AddRange({"London", "Paris", "New York"})
    
            ItemList.Add(Items1)
            ItemList.AddRange({Items2, Items3})
    
            Dim retrieved1 As String = ItemList(0).Item(1)
            Dim retrieved2 As String = ItemList(1).Item(2)
            Dim retrieved3 As String = ItemList(2).Item(0)
    



    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, September 20, 2013 2:26 PM
    • Proposed as answer by Cor Ligthert Friday, September 20, 2013 2:43 PM
    • Marked as answer by Carl CaiModerator Friday, October 4, 2013 2:55 AM
    Friday, September 20, 2013 2:22 PM
  • If you really want a List of Lists:

    Dim ItemList As New List(Of List(Of String))
    ItemList.Add(New List(Of String))
    ItemList(0).Add("Item")
    

    But maybe you want a Dictionary instead?

    Dim ItemList As New Dictionary(Of Integer, String)
    ItemList.Add(90, "Item")


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Friday, September 20, 2013 2:22 PM
  • Dim ItemList As New List(Of List(Of String))
    
    
        Private Sub frmBlack_Market_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
           
    Dim NewList As New List(Of String)
    
            NewList.Add("Item")
            NewList.Add("90")
    
            ItemList.Add(NewList)
    
        End Sub



    Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Friday, September 20, 2013 2:30 PM
  • O sorry so many replies sorry i did not refresh page. Thank you for suggestions/help.

    Let me add this also:

    Anytime that I've seen this used (a List(Of List(Of something))), there's almost always a better way using a Class and one of the properties (or fields) is a List(T).

    For what it's worth. :)


    Please call me Frank :)

    Friday, September 20, 2013 6:00 PM

All replies

  • Hi,

    Do you really want a LIST of Lists?

    Dim list1 as New List(Of String)

    Dim list2 As New List(Of String)

    Dim listOfLists As New List(Of List)

    would probably work.



    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

    Friday, September 20, 2013 2:12 PM
  • Hi

    Here is my take on what you want:

            Dim ItemList As New List(Of List(Of String))
    
            Dim Items1 As New List(Of String)
            Dim Items2 As New List(Of String)
            Dim Items3 As New List(Of String)
            Items1.AddRange({"One", "Two", "Three"})
            Items2.AddRange({"Fred", "Brian", "Mary"})
            Items3.AddRange({"London", "Paris", "New York"})
    
            ItemList.Add(Items1)
            ItemList.AddRange({Items2, Items3})
    
            Dim retrieved1 As String = ItemList(0).Item(1)
            Dim retrieved2 As String = ItemList(1).Item(2)
            Dim retrieved3 As String = ItemList(2).Item(0)
    



    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, September 20, 2013 2:26 PM
    • Proposed as answer by Cor Ligthert Friday, September 20, 2013 2:43 PM
    • Marked as answer by Carl CaiModerator Friday, October 4, 2013 2:55 AM
    Friday, September 20, 2013 2:22 PM
  • If you really want a List of Lists:

    Dim ItemList As New List(Of List(Of String))
    ItemList.Add(New List(Of String))
    ItemList(0).Add("Item")
    

    But maybe you want a Dictionary instead?

    Dim ItemList As New Dictionary(Of Integer, String)
    ItemList.Add(90, "Item")


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Friday, September 20, 2013 2:22 PM
  • Dim ItemList As New List(Of List(Of String))
    
    
        Private Sub frmBlack_Market_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
           
    Dim NewList As New List(Of String)
    
            NewList.Add("Item")
            NewList.Add("90")
    
            ItemList.Add(NewList)
    
        End Sub



    Please remember to 'Mark as Answer' the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Friday, September 20, 2013 2:30 PM
  • I think what you're trying to do is this:

    Dim myList As New List(Of List(Of String))


    Please call me Frank :)

    Friday, September 20, 2013 2:32 PM
  • Following a different sample cant get this to work??

    Dim table1 As DataTable = New DataTable("patients")
        Dim table2 As DataTable = New DataTable("medications")
        Dim set1 As DataSet = New DataSet("office")
    
        Private Sub frmBlack_Market_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            table1.Columns.Add("name")
            table1.Columns.Add("id")
            table1.Rows.Add("sam", 1)
            table1.Rows.Add("mark", 2)
    
    
            table2.Columns.Add("id")
            table2.Columns.Add("medication")
            table2.Rows.Add(1, "atenolol")
            table2.Rows.Add(2, "amoxicillin")
    
            ' Create a DataSet. Put both tables in it.
            set1.Tables.Add(table1)
            set1.Tables.Add(table2)
    
    
            dgvBlackMarket.DataSource = set1
            dgvBlackMarket.Refresh()
        End Sub

    Friday, September 20, 2013 2:34 PM
  • Hi

    You have completely veered off the topic. This seems to be a new question and should be in a new thread.  What happened to your findings regarding this thread on List of Lists?


    Regards Les, Livingston, Scotland

    Friday, September 20, 2013 2:41 PM
  • O sorry so many replies sorry i did not refresh page. Thank you for suggestions/help.
    Friday, September 20, 2013 4:32 PM
  • O sorry so many replies sorry i did not refresh page. Thank you for suggestions/help.

    Let me add this also:

    Anytime that I've seen this used (a List(Of List(Of something))), there's almost always a better way using a Class and one of the properties (or fields) is a List(T).

    For what it's worth. :)


    Please call me Frank :)

    Friday, September 20, 2013 6:00 PM