Ÿ设置从列表System.Windows.Point坐标给出修改错误 [英] Setting Y coordinate of System.Windows.Point from a list gives modify error

查看:84
本文介绍了Ÿ设置从列表System.Windows.Point坐标给出修改错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦,设置一个点的Y坐标,当我访问从列表点。

I am having some trouble setting the Y Coordinate of a Point when I am accessing the Point from a list.

例如,这个工程。

System.Windows.Point test = new System.Windows.Point(6,5);
test.Y = 6;



然而,如果我有点的列表,我通过列表来设定Y访问点协调,我得到一个错误。

However, if I have a list of points and I access a point via the list to set the Y coordinate, I am getting an error.

List<System.Windows.Point> bfunction = new List<System.Windows.Point>();
bfunction.Add(new System.Windows.Point(0, 1));
bfunction[0].Y = 6;



bfunction [0]下划线,给我的的错误不能修改的返回值System.Collections.Generic.List.this [INT],因为它不是一个变量。

The bfunction[0] is underlined and gives me an error of "Cannot modify the return value of 'System.Collections.Generic.List.this[int]' because it is not a variable."

任何帮助,将不胜感激。

Any help would be appreciated.

推荐答案

编译器停止你犯了一个错误,基本上是这样。当您访问 bfunction [0] 将返回点的的复制的。 ,不幸的是(IMO)一个可变结构。因此,如果允许编译您更改的副本,该副本将然后就被扔掉,并声明将是毫无意义的。相反,你需要使用一个变量采取复制,改变它那里,然后把它放回列表:

The compiler is stopping you from making a mistake, basically. When you access bfunction[0] that will return a copy of the point. Point is, unfortunately (IMO) a mutable struct. So if the compiler allowed you to change the copy, that copy would then just be thrown away and the statement would have been pointless. Instead, you need to use a variable to take the copy, change it there, then put it back in the list:

Point point = bfunction[0];
point.Y = 6;
bfunction[0] = point;

这不会是必要的,如果是引用类型,你不会有机会做出错误,如果一直是个的一成不变的值类型来代替。你还不得不需要单独获取和设置,但它会一直是这样的:

This wouldn't be necessary if Point were a reference type, and you wouldn't have had the opportunity to make the error if Point had been an immutable value type instead. You'd still have needed to separately fetch and set, but it would have been something like:

bfunction[0] = bfunction[0].WithY(6);



...其中柔韧将返回其中有相同的 X 值作为原始,但指定的是<一个价值/ code>。

... where WithY would have returned a Point value which had the same X value as the original, but the specified Y.

这篇关于Ÿ设置从列表System.Windows.Point坐标给出修改错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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