If I accumulate data without prior information about the number of elements, I can use the array and increase it with if necessary Redim Preserve, but the List will usually be more efficient. For instance:
Dim vehicle As New List(Of String)(4)
vehicle.Add("car")
vehicle.Add("bicycle")
vehicle.Add("truck")
vehicle.Add("taxi")
vehicle.Add("motorbike")
vehicle.Add("bus")
Although my assumption of 4 as the maximum number of elements was incorrect, I can add new elements without problems.
I can display elements like this:
For inx = 0 To vehicle.Count - 1
Debug.Print(" " & inx & " " & vehicle(inx))
Next
and get:
0 car
1 bicycle
2 truck
3 taxi
4 motorbike
5 bus
I can update items as needed and re-render:
vehicle(2) = "coach"
vehicle(4) = "cart"
For inx = 0 To vehicle.Count - 1
Debug.Print(" " & inx & " " & vehicle(inx))
Next
To obtain:
0 car
1 bicycle
2 coach
3 taxi
4 cart
5 bus
I can create a list of structures almost as easily:
Structure SpersonDtl
Dim familyName As String
Dim givenName As String
Dim age As Integer
End Structure
Dim personDtl As New List(Of SpersonDtl)(4)
Dim personDtlCrnt As SpersonDtl
personDtlCrnt.familyName = "Smith"
personDtlCrnt.givenName = "John"
personDtlCrnt.age = 20
personDtl.Add(personDtlCrnt)
personDtlCrnt.familyName = "Brown"
personDtlCrnt.givenName = "Clare"
personDtlCrnt.age = 21
personDtl.Add(personDtlCrnt)
personDtlCrnt.familyName = "Wilson"
personDtlCrnt.givenName = "David"
personDtlCrnt.age = 22
personDtl.Add(personDtlCrnt)
personDtlCrnt.familyName = "Fox"
personDtlCrnt.givenName = "Wendy"
personDtlCrnt.age = 23
personDtl.Add(personDtlCrnt)
Display list contents with:
For inx = 0 To personDtl.Count - 1
Debug.Print(" " & inx & " " & personDtl(inx).givenName & " " & _
personDtl(inx).familyName & " " & personDtl(inx).age)
Next
gives:
0 John Smith 20
1 Clare Brown 21
2 David Wilson 22
3 Wendy Fox 23
If personDtl was an array, I could easily update the element. To fix Wendy's age, I would write:
personDtl(3).age = 24
personDtl(3).age : " , , ".
, :
Dim personDtlCrnt As SpersonDtl
personDtlCrnt = personDtl(3)
personDtlCrnt.age = 24
personDtl(3) = personDtlCrnt 'Write back.
. , , , .
.