无法为数组中的结构赋值? [英] Can't assign value to structure in array?

查看:78
本文介绍了无法为数组中的结构赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含五个字段的结构。然后我创建一个这种

类型结构的数组,并将结构放入数组元素中。说索引

一个。我想为数组中结构的field3赋值。

当我尝试这个时,出现一个关于延迟赋值的错误。是否可以将
分配给数组中的结构字段?


我正在通过创建一个新的问题解决问题结构,分配

结构字段使用arary中的值...减去字段3,从其他地方获得

值。然后我将这个结构放入数组中。


谢谢,

Brett

I have created a structure with five fields. I then create an array of this
type of structure and place the structure into an array element. Say index
one. I want to assign a value to field3 of the structure inside the array.
When I try this, an error about late assignment appears. Is it possible to
assign a value to a structure field that is in an array?

I''m currently getting around the problem by creating a new structure, assign
the structure fields using values in the arary...minus field 3, which gets a
value from somewhere else. I then place this structure into the array.

Thanks,
Brett

推荐答案

Brett,


这就是你必须这样做的方式。如果你要从一个类别的b / b
上的一个属性返回一个结构,你会遇到同样的问题。由于复印件已退回,您必须存储该复印件,然后

重新分配。


希望这会有所帮助。

- -

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam.guard.caspershouse.com


" Brett" < no@spam.com>在留言中写道

新闻:%2 *************** @ TK2MSFTNGP15.phx.gbl ...
Brett,

That''s pretty much the way that you have to do it. It''s the same
problem you would have if you were to return a structure from a property on
a class. Since a copy is returned, you have to store the copy, then
reassign it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brett" <no@spam.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
我创建了一个有五个领域的结构。然后我创建一个
这种类型的结构数组,并将结构放入一个数组元素。说索引一个。我想为
数组内的结构的field3赋值。当我尝试这个时,会出现关于延迟分配的错误。是否可以为数组中的结构字段赋值?

我正在通过创建新结构解决问题,
分配结构字段使用arary中的值...减去字段3,
从其他地方获取值。然后我将这个结构放入阵列中。

谢谢,
Brett
I have created a structure with five fields. I then create an array of
this type of structure and place the structure into an array element. Say
index one. I want to assign a value to field3 of the structure inside the
array. When I try this, an error about late assignment appears. Is it
possible to assign a value to a structure field that is in an array?

I''m currently getting around the problem by creating a new structure,
assign the structure fields using values in the arary...minus field 3,
which gets a value from somewhere else. I then place this structure into
the array.

Thanks,
Brett



所有结构应该是不可改变的。如果可以以任何可感知的方式更改

结构,那么很可能会错误地使用

。例如,如果从属性返回结构,则可能认为更改结构将改变类。

myClass.myStructProperty.myValue = 5;


这个编译为...


aStruct temp = myClass.myStructProperty;

temp.myValue = 5;

//扔掉温度及其新值。


********


至于你的具体问题,请发布代码。


-

Jonathan Allen

" ;布雷特" < no@spam.com>在留言中写道

新闻:%2 *************** @ TK2MSFTNGP15.phx.gbl ...
All structures should be immutable. If is it possible to change the
structure in any perceivable way, then chances are it will be used
incorrectly. For example, if you return the structure from a property, one
may think that changing the structure will change the class.

myClass.myStructProperty.myValue = 5;

this compiles as...

aStruct temp = myClass.myStructProperty;
temp.myValue = 5;
//throw away temp and its new value.


********

As for you specific problem, post the code.

--
Jonathan Allen
"Brett" <no@spam.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
我创建了一个有五个领域的结构。然后我创建一个
这种类型的结构数组,并将结构放入一个数组元素。说索引一个。我想为
数组内的结构的field3赋值。当我尝试这个时,会出现关于延迟分配的错误。是否可以为数组中的结构字段赋值?

我正在通过创建新结构解决问题,
分配结构字段使用arary中的值...减去字段3,
从其他地方获取值。然后我将这个结构放入阵列中。

谢谢,
Brett
I have created a structure with five fields. I then create an array of
this type of structure and place the structure into an array element. Say
index one. I want to assign a value to field3 of the structure inside the
array. When I try this, an error about late assignment appears. Is it
possible to assign a value to a structure field that is in an array?

I''m currently getting around the problem by creating a new structure,
assign the structure fields using values in the arary...minus field 3,
which gets a value from somewhere else. I then place this structure into
the array.

Thanks,
Brett



好的,你确认这是做到这一点的方法,但为什么?为什么我不能确定
为数组中的结构字段赋值,但是当它们不在数组中时它们可能是什么?

它们不在数组中?


如果它只是一个简单的数组元素,我可以赋值。

谢谢,

Brett

" ; Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com>写在

消息新闻:ep **************** @ TK2MSFTNGP14.phx.gbl ...
Ok, you confirmed that is the way to do it but why?. Why exactly can''t I
assign values to the structure fields that are in the array but can when
they aren''t in the array?

If it were just a simple array element I could assign a value.
Thanks,
Brett
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ep****************@TK2MSFTNGP14.phx.gbl...
Brett,

这就是你必须这样做的方式。如果你要从一个类的属性中返回一个结构,那么你将会遇到同样的问题。由于副本已退回,您必须存储副本,然后重新分配。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com

Brett < no@spam.com>在消息中写道
新闻:%2 *************** @ TK2MSFTNGP15.phx.gbl ...
Brett,

That''s pretty much the way that you have to do it. It''s the same
problem you would have if you were to return a structure from a property
on a class. Since a copy is returned, you have to store the copy, then
reassign it.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brett" <no@spam.com> wrote in message
news:%2***************@TK2MSFTNGP15.phx.gbl...
我创建了一个五层结构领域。然后我创建一个
这种类型的结构数组,并将结构放入一个数组元素。说索引一个。我想为数组里面的结构的field3赋一个值。当我尝试这个时,会出现关于延迟分配的错误。是否可以为数组中的结构字段赋值?

我正在通过创建新结构解决问题,
分配结构字段使用arary中的值...减去字段3,
从其他地方获取值。然后我把这个结构放到阵列中。

谢谢,
Brett
I have created a structure with five fields. I then create an array of
this type of structure and place the structure into an array element. Say
index one. I want to assign a value to field3 of the structure inside
the array. When I try this, an error about late assignment appears. Is it
possible to assign a value to a structure field that is in an array?

I''m currently getting around the problem by creating a new structure,
assign the structure fields using values in the arary...minus field 3,
which gets a value from somewhere else. I then place this structure into
the array.

Thanks,
Brett




这篇关于无法为数组中的结构赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆