none
list (of string)

    Question

  • Hi all, sorry for this silly question but i don't understand.......

    In my code i have an "array of string".

    Dim addresses(20000) As String


    I wanted to use the List(of String) instead.

    so i tried :


    Dim Addresses as new list(of String)


    During exection, it gives me error and i have thought it was because i didn't initialize the list.


    My question is ....

    I can Initialize it as follow :

    Dim addresses As List(Of String) = New List(Of String) From {"", "",""}

    But from the beginning i must know how many items are in the list.

    I know that i must initialize it BEFORE, but how the hell can i use the list(of String), if i DON'T KNOW how many strings are in there ? Will be there ??

    The array in this case is easier. I dim it as a max of 20000 object.

    And with the list(of string) ?????????????

    Thanks a lot.



    Thursday, April 20, 2017 12:32 PM

All replies

  • Leonardo,

    You don't need to know how many in advance; it will dynamically allocate what's needed:

    Dim Addresses As New List(Of String)
    
    ' At some point later:
    
    Addresses.Add("this is a string")


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, April 20, 2017 1:10 PM
  • Hi all, sorry for this silly question but i don't understand.......

    In my code i have an "array of string".

    Dim addresses(20000) As String


    I wanted to use the List(of String) instead.

    so i tried :


    Dim Addresses as new list(of String)


    During exection, it gives me error and i have thought it was because i didn't initialize the list.


    My question is ....

    I can Initialize it as follow :

    Dim addresses As List(Of String) = New List(Of String) From {"", "",""}

    But from the beginning i must know how many items are in the list.

    I know that i must initialize it BEFORE, but how the hell can i use the list(of String), if i DON'T KNOW how many strings are in there ? Will be there ??

    The array in this case is easier. I dim it as a max of 20000 object.

    And with the list(of string) ?????????????

    Thanks a lot.



    Hi

    I don't really know what your problem is. You seem to have an issue over the count of items. Here is some code that shows how to use/count lists.

    There are STOP commands which will pause the execution where you can view the lists/counts by hovering mouse pointer over variables and when ready, use CONTINUE to go to next STOP command.

            Dim addresses1 As New List(Of String)
            Dim addresses2 As New List(Of String)
    
            'these two counts return 0 since the lists are empty
            Dim addressc1 As Integer = addresses1.Count
            Dim addressc2 As Integer = addresses2.Count
            Stop
    
            ' add 4 items - count = 4
            addresses1.AddRange({"1", "2", "3", "4"})
            Dim addresscount1 As Integer = addresses1.Count
            Stop
    
            ' add 1000 items - count = 1000
            For i As Integer = 1 To 1000
                addresses2.Add(i.ToString)
            Next
            Dim addresscount2 As Integer = addresses2.Count
            Stop
    


    Regards Les, Livingston, Scotland

    Thursday, April 20, 2017 1:21 PM
  • Leonardo,

    I was looking for something so that I didn't misquote (it's from a PDF that I've had for a few years).

    Two of them are:

    *****

    41

     
    Use lists instead of arrays when the size is not known in advance

     

    Megha Maheshwari www.linkedin.com/in/formegha www.quora.com/megha-maheshwari

     

     

    When you want to add or remove data, use lists instead of arrays. Lists grow dynamically and dont need to reserve more space than is needed, whereas resizing arrays is expensive. This

    is particularly useful when you know what the pattern of growth is going to be, and what your future pattern of access will be.

    *****

    45

     
    Preallocate sizes on things if you can

     

    Greg Young

    @gregyoung

     

     

    Many objects like memory stream, list, and dictionary will double their size as needed causing wasteful copying and allocations. If you know your list will have 100,000 items,

    initialize it as such to avoid problems in the future.

    *****

    I hope that helps. :)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, April 20, 2017 1:24 PM
  • Thanks a lot !!!!!!!!!!!!!!!!!!!!

    Thursday, April 20, 2017 2:16 PM
  • Thanks a lot !!!!!!!!!!!!!!!!!!!!


    I don't know who you're talking to but if it's to me then you're welcome. :)

    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, April 20, 2017 2:18 PM
  • Hi LeonardoButelli,

    Based on your description, I think you are a bit confused about the generics, please refer to List(of T) for more detailed info.

    Hope it is helpful to you.

    Best regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 21, 2017 1:41 AM
    Moderator
  • I know that i must initialize it BEFORE, but how the hell can i use the list(of String), if i DON'T KNOW how many strings are in there ? Will be there ?

    You have used New to create the List object, so the list itself exists.  But that does not create any list items.  So you need to add items to your list before you can refer to them.   If you attempt to refer to a list item before you add it, then you will get that error.

    An array creates an item (in this case a string) for each element in the array, because it knows how many there will be.  A list does not create any list items - that's something your code needs to do by adding items into the list.

    Friday, April 21, 2017 2:05 AM