这是什么错误,怎么用了属性反而不好用

haizhongfei 2011-07-07 07:25:29
今天碰到一个错误,很奇怪,不是很清楚,
下面是代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Person Tom = new Person();
Tom.Show();
}
}

public class Person
{
private CellLine[] rowLines;
public Person()
{
rowLines = new CellLine[1];
rowLines[0] = new CellLine();
rowLines[0].BeginPoint = new Point(1, 1);
}

public void Show()
{
rowLines[0].beginPoint.X = 99;//没错

//错误: 无法修改“ConsoleApplication1.CellLine.BeginPoint”的返回值,因为它不是变量
rowLines[ 0].BeginPoint.X = 99;

rowLines[ 0].BeginPoint = new Point(1, 1);//也没错
}
};

public class CellLine
{
public Point BeginPoint { get { return beginPoint; } set { beginPoint = value; } }
public Point beginPoint = new Point(0, 0);
};
}
...全文
92 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
老毕 2011-07-07
  • 打赏
  • 举报
回复
尝试修改作为中间表达式的结果生成但未存储在变量中的值类型。 如果尝试直接修改泛型集合中的结构,则会发生此错误。
点击:参考链接
代码养殖员 2011-07-07
  • 打赏
  • 举报
回复
我觉得理论上属性应该是针对它封装的字段进行访问吧,不能对封装的应字段的属性进行操作吧?不知道为什么可以从提示中打出来rowLines[ 0].BeginPoint.X 中的.X
#blackheart 2011-07-07
  • 打赏
  • 举报
回复
那变成引用不报错,,,帮顶!
haizhongfei 2011-07-07
  • 打赏
  • 举报
回复
如果Point不是结构体,换成引用类型后,就没有错误,下面是上面代码的扩充
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;


namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Person Tom = new Person();
Tom.Show();
Console.ReadKey();
}
}

public class Person
{
private CellLine[] rowLines;
public Person()
{
rowLines = new CellLine[1];
rowLines[0] = new CellLine();

rowLines[0].BeginPoint = new Point(1, 1);
rowLines[0].EndPoint = new Node(2, 2);
}

public void Show()
{
rowLines[0].beginPoint.X = 99;//没错
//错误 无法修改“ConsoleApplication1.CellLine.BeginPoint”的返回值,因为它不是变量
rowLines[ 0].BeginPoint.X = 99;
rowLines[ 0].BeginPoint = new Point(1, 1);//没错

rowLines[0].endPoint.X = 88;//没错
rowLines[0].EndPoint.X = 88;//没错
}
};

public class CellLine
{
public Point BeginPoint { get { return beginPoint; } set { beginPoint = value; } }
public Point beginPoint = new Point(0, 0);

public Node EndPoint { get { return endPoint; } set { endPoint = value; } }
public Node endPoint = new Node(0, 0);
};

public class Node
{
public Node()
{ X = 0; Y = 0; }
public Node(int a,int b)
{ X = a; Y = b; }
public int X;
public int Y;
};
}
#blackheart 2011-07-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 haizhongfei 的回复:]

用 VS2010写的,
rowLines[0].beginPoint.X = 99;//没有错
rowLines[0].BeginPoint.X = 99;//就报错,这是为什么
[/Quote]
这个确实很蛋疼,因为你rowLines[0].BeginPoint.X =99,或许编译器认为你给BeginPoint属性赋值,但是还有.X,这估计把编译器搞晕了吧,不晓得给谁赋值了。
我也不怎么明白,坐等,,,
haizhongfei 2011-07-07
  • 打赏
  • 举报
回复
用 VS2010写的,
rowLines[0].beginPoint.X = 99;//没有错
rowLines[0].BeginPoint.X = 99;//就报错,这是为什么
#blackheart 2011-07-07
  • 打赏
  • 举报
回复
丑陋的解决办法:
rowLines[0].BeginPoint=new Point(99,rowLines[0].BeginPoint.Y);
#blackheart 2011-07-07
  • 打赏
  • 举报
回复
rowLines[0].BeginPoint是属性,本质是方法,所以方法是不会再有属性的(X),
Snowdust 2011-07-07
  • 打赏
  • 举报
回复
你定义的属性BeginPoint是个Point类型,必须以一个Point的整体进行赋值或者取值,那为什么可以用
int n = rowLines[0].BeginPoint.X进行访问呢?
实际上取值的时候也是以一个整体进行取值的,只是取到的值具有X属性,所以这样可以用。
相当于
Point p = rowLines[0].BeginPoint;
int n = p.X;
所以还是以一个整体进行取值的。
而beginPoint是一个public类型的变量,当然可以直接访问X属性了。
不知道我说清楚了没有。
燃烧土豆 2011-07-07
  • 打赏
  • 举报
回复
你这是用VS2010写的??
燃烧土豆 2011-07-07
  • 打赏
  • 举报
回复
好样的百分贴,等着让我看看

110,530

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧