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
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).
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
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.
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.
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
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:blush:
Yes generics would be like C++'s template classes.