Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Getting this error on page load

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

C#
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

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


Any suggestions, please?
Posted
Comments
Tomas Takac 6-Jan-16 4:51am    
I think the error message is clear enough. What is your question?
Member 9017207 6-Jan-16 4:52am    
What can I do to solve it?

You could use a temporary variable to make this work, like this:
C#
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.
 
Share this answer
 
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.
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900