您的位置:首页 > 产品设计 > UI/UE

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

//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);
}
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐