Results 1 to 7 of 7

Thread: ArrayList vs Array of Strings?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    263

    ArrayList vs Array of Strings?

    If I want to store a list of names ie. "David", "Frank", "Chris", would it be better to do:

    Code:
     Dim al As New ArrayList()
    
      al.Add("David")
      al.Add("Frank")
      al.Add("Chris")
    or

    Code:
     Dim names(2) As string
    names(0) = "David"
    names(1) = "Frank"
    names(2) = "Chris"
    Also, what is the difference if you put the () with the variable name or with the type? I see sometimes
    Code:
     Dim names(2) As string
    and other times
    Code:
     Dim names As string()
    Thanks!

    Dave

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: ArrayList vs Array of Strings?

    ArrayLists are collections, which means that you can add / remove elements to it at runtime. If you know what exact amount of names you'll be storing, use an array....if not, use the ArrayList (however if you're using .NET 2.0 or above you should use a List(Of String) instead).
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    263

    Re: ArrayList vs Array of Strings?

    It seems to let me do this:
    Code:
    Dim AssociateList() As TTDataSet.AssociateTableRow = DirectCast(AssociateTable.Select(FilterString, SortString), TTDataSet.AssociateTableRow())
    
    Dim NameList(AssociateList.Length - 1) As String
    That quantity is determined at runtime, and it still lets me declare an array - why is that?.

    Also, I tried this
    Code:
    Dim col As New List(Of String)
    and it said 'Type List is not defined' and it recommended I import system.collections.generic. Isn't that like a "late binding" thing? If you know the type before hand, isn't it a good idea NOT to use late binding?

    Thanks,

    Dave

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: ArrayList vs Array of Strings?

    Yes the quantity would be determined at runtime, but thats no problem because the array is instantiated at runtime aswell.

    Importing a namespace is not late binding. You could just write:
    Code:
    Dim col As New System.Collections.Generic.List(Of String)
    But in order to avoid specifying the entire namespace path, you can import the System.Collections.Generic namespace, and only specify List(Of String) as type.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: ArrayList vs Array of Strings?

    It's not late binding, it is generics. Generics are basically classes that can be instantiated and told what type of data they are working with. Late binding is not knowing the type until runtime, here you know the type at design time.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2004
    Posts
    263

    Re: ArrayList vs Array of Strings?

    I see, I thought generics=late binding. So generics are like template classes in c++?

    Thanks for the explanations.
    Dave

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: ArrayList vs Array of Strings?

    Oh you where refering to the generics when you mentioned late binding, i thought you where talking about importing that namespace

    Yes generics would be like C++'s template classes.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

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