vb.net 如何在VB.NET中为List中的结构元素赋值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7126318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:40:05  来源:igfitidea点击:

How do you assign values to structure elements in a List in VB.NET?

vb.netdata-structures

提问by Chad Harrison

I have a user-defined structure in a list that I am trying to change the value for in an individual element within the list of structures. Accessing the element is not a problem. However, when I try to update the value, the compiler complains:

我在列表中有一个用户定义的结构,我试图更改结构列表中单个元素的值。访问元素不是问题。但是,当我尝试更新该值时,编译器会抱怨:

"Expression is a value and therefore cannot be the target of the assignment"

“表达式是一个值,因此不能成为赋值的目标”

For example:

例如:

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

采纳答案by Chris Dunaway

As the other comments said, when you refer to records(0), you get a copy of the struct since it is a value type. What you can do (if you can't change it to a Class) is something like this:

正如其他评论所说,当您引用记录(0)时,您将获得该结构的副本,因为它是一种值类型。你可以做的(如果你不能把它改成一个类)是这样的:

Dim p As Person = records(0)
p.first = "Papa"
records(0) = p

Although, I think it's just easier to use a Class.

虽然,我认为使用类更容易。

回答by Dan Tao

There are actually twoimportant concepts to remember here.

这里实际上有两个重要的概念需要记住。

One is that, as Hans and Chrishave pointed out, Structure Persondeclares a value typeof which copiesare passed between method calls.

一个是,正如 Hans 和Chris所指出的,Structure Person声明了一个值类型,其副本在方法调用之间传递。

You can still access (i.e., get andset) the members of a value type, though. After all, thisworks:

不过,您仍然可以访问(即,获取设置)值类型的成员。毕竟,有效:

Dim people(0) As Person
people(0).first = "Yogi"
people(0).last = "Bear"
people(0).age = 35

So the otherimportant point to realize is that records(0)accesses the List(Of Person)class's special Itemproperty, which is a sugary wrapper around two method calls (a getter and setter). It is nota direct array access; if it were (i.e., if recordswere an array), your original code would actually have worked.

所以要实现的另一个重点是records(0)访问List(Of Person)类的特殊Item属性,它是两个方法调用(getter 和 setter)的甜蜜包装器。它不是直接的数组访问;如果是(即,如果records是数组),您的原始代码实际上会起作用。

回答by Giorgio Acquati

I had the same problem, and I fixed it by adding a simple Sub to the structure that changes the value of the property.

我遇到了同样的问题,我通过向更改属性值的结构添加一个简单的 Sub 来修复它。

Public Structure Person

 Dim first as String
 Dim last as String
 Dim age as Integer

 Public Sub ChangeFirst(value as String)
  me.first = value
 End Sub

End Structure