Cannot modify the return value of ...,because it is not a variable 错误分析与解决
2010-12-06 21:34
489 查看
今天,写程序的时候,遇到Cannotmodifythereturnvalueof...,becauseitisnotavariable这个错误.纠结了一个多小时,.因为按我人常规去写程序,程序看起来是对的.
但编译会提示出这个错误.
后来,要csnd上找到了微软的解释.是这样的.
---------------------------------------------------------------------------------------------
错误出现的原因:
在类中定义了结构体,结构体有C#库中的实例,然后在后面的过程以试图对这个实例进行修改是个会出现.
需要注意的是:
<1>必须是在类中定义的结构体;
<2>结构体中必须包含了C#类库中的实例;
注意:这里说的实例只包含直接实例,比如:Point实例,不包含序列实例,比如List<Point>也就是说后面这种是好使的,原因会在后面的分析中说明.
<3>在后面的代码中尝试了对该实例修改.
---------------------------------------------------------------------------------------------
错误描述与分析:
错误出现的原因是:C#.Struct中对C#类的实例的访问操作返回的是副本(中间值),也就是说,直接的调用是只读的,VS编译器直接拒绝这么做,所以给出提示(因为它本身的值是没有改变,改变的只是中间值).而对于容器类型的,在struct中它直接就是以引用形式存在的,所以不会出发现这种问题.具体例子参考:
csdn:http://msdn.microsoft.com/en-us/library/wydkhw2c(vs.71).aspx
如下:
错误的解决方法:
<1>可以使用class代替struct(推荐)
<2>其它方法就要从它出现的原因分析入手了.
csdn中的描述如下:
Cannotmodifythereturnvalueof'expression'becauseitisnotavariable
Anattemptwasmadetomodifyavaluetypethatwastheresultofanintermediateexpression.Becausethevalueisnotpersisted,thevaluewillbeunchanged.
Toresolvethiserror,storetheresultoftheexpressioninanintermediatevalue,oruseareferencetypefortheintermediateexpression.
ThefollowingsamplegeneratesCS1612:
Copy
但编译会提示出这个错误.
后来,要csnd上找到了微软的解释.是这样的.
---------------------------------------------------------------------------------------------
错误出现的原因:
在类中定义了结构体,结构体有C#库中的实例,然后在后面的过程以试图对这个实例进行修改是个会出现.
需要注意的是:
<1>必须是在类中定义的结构体;
<2>结构体中必须包含了C#类库中的实例;
注意:这里说的实例只包含直接实例,比如:Point实例,不包含序列实例,比如List<Point>也就是说后面这种是好使的,原因会在后面的分析中说明.
<3>在后面的代码中尝试了对该实例修改.
---------------------------------------------------------------------------------------------
错误描述与分析:
错误出现的原因是:C#.Struct中对C#类的实例的访问操作返回的是副本(中间值),也就是说,直接的调用是只读的,VS编译器直接拒绝这么做,所以给出提示(因为它本身的值是没有改变,改变的只是中间值).而对于容器类型的,在struct中它直接就是以引用形式存在的,所以不会出发现这种问题.具体例子参考:
csdn:
如下:
错误的解决方法:
<1>可以使用class代替struct(推荐)
<2>其它方法就要从它出现的原因分析入手了.
csdn中的描述如下:
Cannotmodifythereturnvalueof'expression'becauseitisnotavariable
Anattemptwasmadetomodifyavaluetypethatwastheresultofanintermediateexpression.Becausethevalueisnotpersisted,thevaluewillbeunchanged.
Toresolvethiserror,storetheresultoftheexpressioninanintermediatevalue,oruseareferencetypefortheintermediateexpression.
ThefollowingsamplegeneratesCS1612:
//CS1612.cs classReference { publicinti; publicintProp { get { returni; } set { i=value; } } publicoverridestringToString() { return"i="+i.ToString(); } } structValue { publicinti; publicintProp { get { returni; } set { i=value; } } publicoverridestringToString() { return"i="+i.ToString(); } } classExample { privateReference_r;//Referencetype privateValue_v;//Valuetype publicExample() { _r=newReference(); _v=newValue(); } publicReferenceRefProp { get { return_r; } set { _r=value; } } publicValueValueProp { get { return_v; } set { _v=value; } } publicstaticvoidMain() { Examplee=newExample(); System.Console.WriteLine(e.RefProp);//Prints0 e.RefProp.i=5;//Legal System.Console.WriteLine(e.RefProp);//Prints5 e.RefProp.Prop=6;//Legal System.Console.WriteLine(e.RefProp);//Prints6 System.Console.WriteLine(e.ValueProp);//Prints0 //InvalidbecauseValuePropreturnsavalueonthestack, //andalthoughyoucouldsetthatvalue, //itdoesn'treflectbackintotheoriginalproperty. e.ValueProp.i=7;//CS1612 //Iftheabovestatmentwasallowed,thiswouldstillprint0. System.Console.WriteLine(e.ValueProp); //InvalidbecauseValuePropreturnsavalueonthestack, //andalthoughyoucouldsetthatvalue, //itdoesn'treflectbackintotheoriginalproperty. e.ValueProp.Prop=8;//CS1612 //Iftheabovestatementwasallowed,thiswouldstillprint0. System.Console.WriteLine(e.ValueProp); Valuev=e.ValueProp; v.i=9; System.Console.WriteLine(v);//Prints9 System.Console.WriteLine(e.ValueProp);//Stillprints0 v.Prop=10; System.Console.WriteLine(v);//Prints10 System.Console.WriteLine(e.ValueProp);//Stillprints0 e.ValueProp=v; //Thefollowinglineprints10(thisistheproperway). System.Console.WriteLine(e.ValueProp); } }
相关文章推荐
- “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
- ECShop后台站点地图关于 Deprecated: Assigning the return value of new by reference is deprecated的错误的解决办法
- 解决Eclipse插件升级中Cannot complete the install because of a conflicting dependency的问题
- Eclipse:Cannot complete the install because of a conflicting dependency.问题解决
- 解决Eclipse插件升级中Cannot complete the install because of a conflicting dependency的问题
- PHP显示Deprecated: Assigning the return value of new by reference is deprecated in解决办法
- 解决Eclipse插件升级中Cannot complete the install because of a conflicting dependency的问题
- 23. Can't connect to X11 window server using '127.0.0.1:0.0' as the value of the DISPLAY variable.解决办法
- mysql:Variable 'character_set_client' can't be set to the value of 'NULL'解决
- 解决Deprecated: Assigning the return value of new by reference
- Eclipse:Cannot complete the install because of a conflicting dependency.问题解决
- 关于Assigning the return value of new by reference is deprecated的错误提示
- 错误解决:Some projects cannot be imported because they already exist in the workspace
- 显示Deprecated: Assigning the return value of new by reference is deprecated in解决办法
- ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined. 错误解决方法
- Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call.... 此错误的解决方法
- 解决错误 Some projects cannot be imported because they already exist in the workspace
- Deprecated: Assigning the return value of new by reference is deprecated in……【解决方法】
- Eclipse:Cannot complete the install because of a conflicting dependency.问题解决