Passing the Session object as a reference

I wanted to pass the Session object to a method but received an error message:

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

int result = something.ValidateUser("someIP","someuser","somepass","somemodule", ref Session, out ErrorText);

Alright. So I tried this:

int result = something.ValidateUser("someIP","someuser","somepass","somemodule", Session, out ErrorText);

Which I accepted like this:

public int ValidateUser(string UserHostAddress, string UserName, string UserPassword, string ServiceCode, System.Web.SessionState.HttpSessionState MySession, out string ErrorText)

{

...

MySession["my_incredibly_precious_variable"] = "some value";
...
}

And then, outside this method, in my main:

Session["my_incredibly_precious_variable"] was indeed holding "some value".

How is this possible? And most importatnly: is this PROPER?

I couldn't pass Session as a reference or out, so I'm not sure I should be doing this?

Thanks in advance!

Regards,
Kyanzes
KyanzesAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
bklmnshConnect With a Mentor Commented:
HttpContext.Current.Session or Page.Session hold the same HttpSessionState object, you don't need to pass it by reference and this is correct.
You can change
MySession["my_incredibly_precious_variable"] = "some value";
to
HttpContext.Current.Session["my_incredibly_precious_variable"] = "some value";
and you don't need to pass HttpSessionState object as a parameter at all.
0
 
WesWilsonCommented:
Kyanzes, the simple answer is that by default you are passing by reference whenever you pass an object.

As bklmnsh indicates, within the ASP.NET application there should be no need to pass the Session, as it persists and can be accessed elsewhere.
0
 
KyanzesAuthor Commented:
Thanks!
0
All Courses

From novice to tech pro — start learning today.