tags:

views:

176

answers:

3

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


    }
}
 
+3  A: 

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)).

Marc Gravell
+3  A: 

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.

Jon Skeet
d'oh! just as I add the same link in ;-p
Marc Gravell
I'm not going to object to two links to the same article :)
Jon Skeet
A: 

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.

Brian Rudolph