“Cannot modify the return value of” “because it is not a variable”
来源:互联网 发布:淘宝点击率公式 编辑:程序博客网 时间:2024/05/16 04:27
编译错误 CS1612
Cannot modify the return value of ‘expression‘ because it is not a variable
无法修改“expression”的返回值,因为它不是变量
出现这种错误的最常见情况是:
AnObject.AnStruct.Vaule = xxx;
考虑如下程序:初看没什么问题,实际上根本无法编译
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
public struct Rectangle
{
private int _Width;
private int _Height;
public int Width
{
get { return _Width; }
set { _Width = value; }
}
public int Height
{
get { return _Height; }
set { _Height = value; }
}
public Rectangle(int width, int height)
{
this._Width = width;
this._Height = height;
}
}
public class Draw
{
private Rectangle _rect = new Rectangle(0, 0);
public Rectangle MyRect
{
get { return _rect; }
set { _rect = value; }
}
public Draw() { }
public Draw(int mapWidth, int mapHeight)
{
_rect = new Rectangle(mapWidth, mapHeight);
}
}
class Program
{
static void Main(string[] args)
{
Draw draw = new Draw();
draw.MyRect.Width = 20;//《== 编译错误
draw.MyRect.Height = 12;《== 编译错误
}
}
}
draw是引用类型,MyRect是struct,也就是值类型,draw.MyRect会调用
public Rectangle MyRect
{
get { return _rect; }
}
而值类型默认是按值传递的,返回的是栈上_rect的一份临时的、本地的拷贝,我们暂且称之为temp_rect,
draw.MyRect.Width = 20 等同于temp_rect.Width = 20;
因此即使能对它进行修改也无法反映到draw.MyRect本身(即_rect)的,即这是一个毫无何意义的操作,因此编译器就从源头上禁止了这样的操作。
解决方法:
1)把struct替换成class,这样,MyRect就成了一个对象(引用类型),draw.MyRect就会返回_rect在堆上的实际地址,修改也会反映到实际的对象
2)如果非要用struct不可的话,需要设置一个中间变量:
Draw draw = new Draw();
Rectangle tempRect = new Rectangle();
tempRect.Width = 20;
tempRect.Height = 12;
draw.MyRect = tempRect;
这样实际上是替换draw对象的整个_rect属性。
参考:
MSDN: Compiler Error CS1612 (编译器错误 CS1612 (C#) )
看一下就知道了,中文的比英文原版的逊多了
C# - Struct in a class.. can not access properties of the struct
延伸阅读:
CLR Generics Limitation - Modifying Values In Situ In a Container
C#: List of struct
Cannot modify the return value of ‘expression‘ because it is not a variable
无法修改“expression”的返回值,因为它不是变量
出现这种错误的最常见情况是:
AnObject.AnStruct.Vaule = xxx;
考虑如下程序:初看没什么问题,实际上根本无法编译
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
public struct Rectangle
{
private int _Width;
private int _Height;
public int Width
{
get { return _Width; }
set { _Width = value; }
}
public int Height
{
get { return _Height; }
set { _Height = value; }
}
public Rectangle(int width, int height)
{
this._Width = width;
this._Height = height;
}
}
public class Draw
{
private Rectangle _rect = new Rectangle(0, 0);
public Rectangle MyRect
{
get { return _rect; }
set { _rect = value; }
}
public Draw() { }
public Draw(int mapWidth, int mapHeight)
{
_rect = new Rectangle(mapWidth, mapHeight);
}
}
class Program
{
static void Main(string[] args)
{
Draw draw = new Draw();
draw.MyRect.Width = 20;//《== 编译错误
draw.MyRect.Height = 12;《== 编译错误
}
}
}
draw是引用类型,MyRect是struct,也就是值类型,draw.MyRect会调用
public Rectangle MyRect
{
get { return _rect; }
}
而值类型默认是按值传递的,返回的是栈上_rect的一份临时的、本地的拷贝,我们暂且称之为temp_rect,
draw.MyRect.Width = 20 等同于temp_rect.Width = 20;
因此即使能对它进行修改也无法反映到draw.MyRect本身(即_rect)的,即这是一个毫无何意义的操作,因此编译器就从源头上禁止了这样的操作。
解决方法:
1)把struct替换成class,这样,MyRect就成了一个对象(引用类型),draw.MyRect就会返回_rect在堆上的实际地址,修改也会反映到实际的对象
2)如果非要用struct不可的话,需要设置一个中间变量:
Draw draw = new Draw();
Rectangle tempRect = new Rectangle();
tempRect.Width = 20;
tempRect.Height = 12;
draw.MyRect = tempRect;
这样实际上是替换draw对象的整个_rect属性。
参考:
MSDN: Compiler Error CS1612 (编译器错误 CS1612 (C#) )
看一下就知道了,中文的比英文原版的逊多了
C# - Struct in a class.. can not access properties of the struct
延伸阅读:
CLR Generics Limitation - Modifying Values In Situ In a Container
C#: List of struct
- “Cannot modify the return value of” “because it is not a variable”
- Cannot modify the return value of ...,because it is not a variable 错误分析与解决
- Cannot modify the return value of 'System...ClientRectangle' because it is not a variable
- ADODB使用excel的"Cannot modify the design of table 'xxxx'. It is in a read-only database"的问题
- the environment variable java_home (with the value of ) does not point to a valid jvm installation
- BACKSLASH RIGHT: SLASH This is a slash: /. Because the top of it leans forward, it is sometimes
- It is because of the existence of this spell, t
- Can not delete a master page "This item cannot be deleted because it is still referenced by other pa
- the behavior of the UICollectionViewFlowLayout is not defined because:
- 【iOS】The behavior of the UICollectionViewFlowLayout is not defined because:
- The behavior of the UICollectionViewFlowLayout is not defined because 解决办法!
- the command could not be located because '/bin' is not included in the path environment variable
- The command could not be located because '/sbin' is not included in the PATH environment variable
- the app xxx.app could not be added to your iTunes library because it is not a valid app
- The environment variable JAVA_HOME(with the value of xxx) does not point to a valid JVM installation
- Ubuntu Cannot complete the install because of a conflicting dependency.
- Cannot complete the install because of a conflicting dependency.
- cheap louboutin shoes It is said that a couple of lofty heels namely the important"weapon"because al
- 在SQL语句中,如何去掉小数点后面多余的零?
- Shell脚本调试技术
- 根据某一字段跨服务器比较两个表数据
- 软件配置管理和文档管理
- 计算机出现“蓝屏”的可能原因
- “Cannot modify the return value of” “because it is not a variable”
- Visual C++ MFC 简明教程1:MFC导论
- 开关电源和线性电源比较
- linux svn命令大全
- ACE线程管理机制-并发控制(2)
- 判断数据库中是否存在表
- jquery 不同浏览器判断颜色值的方法
- 自己动手编译OpenJDK 7(转载)
- 对象****中的结构体