title: Is it possible to update elements within a List(Of Structure)?
body: <p>If I am accumulating data with no advance information about the number of elements, I can use an array and enlarge it as necessary using <code>Redim Preserve</code>, but a <a href="http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.85%29.aspx" rel="nofollow">List</a> will usually be more efficient. For example:</p>
  
  <pre><code>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")
  </code></pre>
  
  <p>Even though my guess of 4 as the maximum number of elements was wrong, I can add new elements without problems.</p>
  
  <p>I can display the elements so:</p>
  
  <pre><code>For inx = 0 To vehicle.Count - 1
      Debug.Print("    " &amp; inx &amp; " " &amp; vehicle(inx))
  Next
  </code></pre>
  
  <p>and get:</p>
  
  <pre><code>0 car
  1 bicycle
  2 truck
  3 taxi
  4 motorbike
  5 bus
  </code></pre>
  
  <p>I can update elements as required and redisplay:</p>
  
  <pre><code>vehicle(2) = "coach"
  vehicle(4) = "cart"
  
  For inx = 0 To vehicle.Count - 1
      Debug.Print("    " &amp; inx &amp; " " &amp; vehicle(inx))
  Next
  </code></pre>
  
  <p>to get:</p>
  
  <pre><code>0 car
  1 bicycle
  2 coach
  3 taxi
  4 cart
  5 bus
  </code></pre>
  
  <p>I can create a list of structures almost as easily:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Displaying the list's contents with:</p>
  
  <pre><code>For inx = 0 To personDtl.Count - 1
      Debug.Print("    " &amp; inx &amp; " " &amp; personDtl(inx).givenName &amp; " " &amp; _
                  personDtl(inx).familyName &amp; " " &amp; personDtl(inx).age)
  Next
  </code></pre>
  
  <p>gives:</p>
  
  <pre><code>0 John Smith 20
  1 Clare Brown 21
  2 David Wilson 22
  3 Wendy Fox 23
  </code></pre>
  
  <p>If personDtl were an array, I could update an element easily.  To correct Wendy's age, I would write:</p>
  
  <pre><code>personDtl(3).age = 24
  </code></pre>
  
  <p>However, the same statement with a list, results in a blue line under <code>personDtl(3).age</code> and the error message: "<em>Expression is a value and therefore cannot be the target of an assignment.</em>"</p>
  
  <p>The best solution I have found is:</p>
  
  <pre><code>Dim personDtlCrnt As SpersonDtl
  
  personDtlCrnt = personDtl(3)
  personDtlCrnt.age = 24
  personDtl(3) = personDtlCrnt 'Write back.
  </code></pre>
  
  <p>In my application, I am accumulating information into large, complex structures.  To copy an element out of the list, add one new item of information and then copy it back would not be an efficient process.</p>
  
  <p>I would be grateful for any suggestions for an alternative approach.</p>