我正在使用类似于下面的结构。我需要遍历'Persons'ArrayList并将每个薪水设置为100,同时保持LastNames不变。VB.Net如何更新ArrayList中的每个条目?
Structure Person
Dim LastName As String
Dim salary As Integer
End Structure
public class Test
public Shared Sub Main
Dim Persons As New ArrayList
Dim Person As New Person
With Person
.LastName = "Smith"
.salary = 50
End With
Persons.Add(Person)
With Person
.LastName = "Jones"
.salary = 20
End With
Persons.Add(Person)
With Person
.LastName = "Brown"
.salary = 80
End With
Persons.Add(Person)
End Sub
End class
我意识到一个简单的For Each循环在这里不起作用。我可以将每个'Person'复制到第二个临时数组列表中,然后删除原数据列表中的条目,但我无法弄清楚如何更改每个人的工资,并在保留'LastName'的同时'再次'添加'价值观就像它们最初一样。
具有合适属性的类比这里的结构更合适;同样,一个类型化的集合像一个'List(Of T)'来代替无类型的ArrayList。然后循环 – Plutonix
@Plutonix说什么。 ArrayList属于C#没有泛型的日子。它已弃用,获得列表。除非您必须与使用它的旧API接口,否则不应在目标.NET> = 2.0的新代码中使用ArrayList。从http://stackoverflow.com/a/2309699/832052 –
djv
[在类和结构之间选择](https://msdn.microsoft.com/en-us/library/ms229017(v = vs.110).aspx) – Plutonix