Sandhurst
Sandhurst

Reputation:

How to pass Page as ref parameter to a function

I would like to know how Can I paas Page as Ref Parameter to a function

This is what I want to do

 
  public partial class HomePage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!SetupUserPermission.isSessionExpired())
        {
            string UserId = Session["UserId"].ToString();
            SetupUserPermission.SetupUserRights(ref this.Page, Convert.ToInt32(UserId));
        }


    }
}
 

Upvotes: 2

Views: 3799

Answers (3)

Brian Rudolph
Brian Rudolph

Reputation: 6308

Unless you need to alter the reference of this.Page in downstream calls and have the reference reflect the changes upstream, there is no reason to pass this by ref. Either way i don't think this works with properties, especially get only ones like this.Page.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499810

You can't pass a property by reference in C#. Why do you want to pass Page by reference in this case?

In VB you can pass a property by reference, and the equivalent in this case would be:

Page tmp = Page;
SetupUserPermission.SetupUserRights(ref tmp, Convert.ToInt32(UserId));
Page = tmp;

Are you really sure you want to do that?

I suspect you don't really want to pass it by reference, and you're just slightly confused about parameter passing. See my article on the topic for more information.

Upvotes: 4

Marc Gravell
Marc Gravell

Reputation: 1062520

Why do you want to pass it ref? It seems to me that a regular pass should do; this passes the reference by value - which is what you want (unless you are creating a new page...).

Also, isn't "this" the Page? Can't you just:

SetupUserPermission.SetupUserRights(this, ...);

where SetupUserRights takes a Page?

See also: Jon Skeet's page on parameter passing in C#; that might fix a few misconceptions (hint: Page is a reference-type (a class)).

Upvotes: 3

Related Questions