如何解决属性或索引器可能不会作为out或ref参数错误传递 [英] How do I solve A property or indexer may not be passed as an out or ref parameter error

查看:1061
本文介绍了如何解决属性或索引器可能不会作为out或ref参数错误传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在页面加载时出现此错误



属性或索引器不能作为out或ref参数传递



  private   void  Page_Load( object  sender,EventArgs e)
{
Page oPage = this .Page;
this .oDB.Init_Object( ref this .Session, ref this .Response, ref oPage, ref .oUser);
this .Page = oPage;
this .oConnInfo =(ConnInfo) this .Session [ oConnInfo];
this .ReqNbr =( int )Math.Round(Conversion.Val(Strings.Trim) ( this .Request.QueryString [ R])));
this .BindReport();
.btnPrint_Click();
}





基本上错误来自以下行



  this  .oDB.Init_Object(  ref   this  .Session, ref   this  .Response, ref  oPage, ref   this  .oUser); 





有什么建议吗?

解决方案

您可以使用临时变量来完成这项工作,如下所示:

  int  temp =  .SomeProperty; 
SomeMethodCall( ref temp);
这个 .SomeProperty = temp;





另外一个请注意,如果可能,我建议您避免 ref out 参数。这并不是说它们本身就是坏但过度使用它们会使你的代码难以理解,并且引入bug的风险也会增加。在这里删除ref参数的一个选项可能是将Session,Response,oPage和oUser封装在一个类中,然后可以将其作为参数传递而不用 ref


< blockquote>属性和索引器不是值 - 它们是幕后方法调用的语法糖 - 因此不必同时具有get参数所需的getter和setter。



相反,声明变量并传递它们,就像你现在传递Page一样。


当你传递ref参数时您需要将数据放入该变量的时间。如果您不想放置数据,可以使用Out参数。


Getting this error on page load

"A property or indexer may not be passed as an out or ref parameter"

private void Page_Load(object sender, EventArgs e)
        {
            Page oPage = this.Page;
            this.oDB.Init_Object("", ref this.Session, ref this.Response, ref oPage, ref this.oUser);
            this.Page = oPage;
            this.oConnInfo = (ConnInfo) this.Session["oConnInfo"];
            this.ReqNbr = (int) Math.Round(Conversion.Val(Strings.Trim(this.Request.QueryString["R"])));
            this.BindReport();
            this.btnPrint_Click();
        }



Basically the error is coming on the below line

this.oDB.Init_Object("", ref this.Session, ref this.Response, ref oPage, ref this.oUser);



Any suggestions, please?

解决方案

You could use a temporary variable to make this work, like this:

int temp = this.SomeProperty;
SomeMethodCall(ref temp);
this.SomeProperty = temp;



On a different note, I would advise you to avoid ref and out parameters if possible. It's not like they're inherently "bad" but overusing them will make your code hard to understand and the risk of introducing bugs increases. An option to remove your ref-parameters here might be to encapsulate Session, Response, oPage and oUser in a class which you can then pass as a parameter without ref.


Properties and indexers are not values - they are syntactic sugar for method calls behind the scenes - and as such don't have to have both a getter and a setter, which is needed for a ref parameter.

Instead, declare variables and pass those instead, in the same way you are passing the Page at the moment.


When you pass the ref parameter at the time you required to put the data into that variable. If you dont want to put the data you can use the Out parameter.


这篇关于如何解决属性或索引器可能不会作为out或ref参数错误传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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