我在列表中有一个用户定义的结构,我试图在结构列表中的单个元素中更改值.访问元素不是问题.但是,当我尝试更新该值时,编译器抱怨:
“Expression is a value and therefore cannot be the target of the
assignment”
例如:
Public Structure Person Dim first as String Dim last as String Dim age as Integer End Structure
_
Public Sub ListTest() Dim newPerson as Person Dim records as List (Of Person) records = new List (Of Person) person.first = "Yogi" person.last = "bear" person.age = 35 records.Add(person) records(0).first = "Papa" ' <<== Causes the error End Sub
正如其他评论所说,当您引用记录(0)时,您将获得一个结构体的副本,因为它是一个值类型.你可以做什么(如果你不能把它改成一个类)就是这样的:
Dim p As Person = records(0) p.first = "Papa" records(0) = p
虽然,我认为使用一个类更简单.