Search code examples
vb.netloopsgenericsdynamicuser-input

How to create list of t and name it in VB.net


I have a textbox where the user can enter a number. Based on that number, the same amount of List(Of T) should be created!

How can I dynamically create Lists using List(Of T) and giving each List(Of T) a certain name so I can access them later on?

For i = 0 to (txtLoopNumber.Text)

    Dim ThisIsAList As New List(Of String)()

Next

For instance, the user enters "3" in the txtLoopNumber. So the loop above should be three times. How can I make the code so that it creates "ThisIsAList1", "ThisIsAList2", "ThisIsAList3"?

My ideas on solving this problem

Since I cannot assign a name or a tag I was thinking about creating listboxes where I got the property "name"???? But this doesn't seem like the most effective solution to me


Solution

  • As @Plutonix said, you can better achieve this by the indexer of a List(Of List(Of T):

    Dim lists As New List(Of List(Of String))
    Dim amount As Integer = CInt(txtLoopNumber.Text)
    
    For i As Integer = 0 To (amount - 1)
        lists.Add(New List(Of String))
    Next i
    
    lists(0).Add("QWERTY1")
    lists(1).Add("QWERTY2")
    
    Console.WriteLine(lists(0)(0))
    Console.WriteLine(lists(1)(0))