List<Foo> fooList = Session["foo"] as List<Foo>;
fooList.Add(bar);
Does the call to Add() change the data that's in the session? Put another way: when I next pull "foo" from the Session, will the list contain bar?
Yes the session will be changed as a List<T> is a reference type. All that this fooList variable represents is a pointer to the real object and all that Session["foo"] represents is also a pointer to the same object. So changing fooList will affect the real object that the session is also pointing to. The behavior will be different if you store value types in session.
Related
I have an ASP.NET application which uses InProc session state mode. I have a class marked as serializable:
namespace MyNamespace
{
[Serializable]
public class MyClass
{
public System.Web.Mvc.SelectList myList { get; set; }
}
}
...Now I have a doubt: When assigning the property value within the serialized class to the session object like below:
Session["MyList"] = InstaceOfMyClass.myList;
... is myList property automatically serialized once inserted to Session["MyList"] object?
In case it is not serialized, how can I serialize it on inserting to Session["MyList"] object? by indicate property with serializable attribute?
If you are talking about in-process only, serialisation doesn't happen, the item is stored as a normal live reference to a .NET object, no serialisation happens.
If your session state is external (e.g. database) then obviously they need to be serialised at that point.
With regards to your specific point, there is no difference between what you are doing and creating the SelectList object manually and passing that into the session.
If I were you, I would strongly recommend that you do not store objects like this in the session, try to keep it to classes that you control (and can mark as Serializable), because if you do need to switch to out-of-process storage, you won't need to re-engineer your code.
Serialization and passing a model (and some extra data) from the controller to the view (as indicated by the System.Web.Mvc.SelectList) are two very different things.
Serialization has to do with turning an object into a string (or binary array) and Deserialization back from a string (or binary array) to an object.
The model passed back and forth on MVC is what the view should render. You're probably looking for a single property in that model (say countryID) to be displayed in a drop down list (or an html <select> or more generally a combo box). This bundle of wrapping data (say - the list of countries - Id and name pairs) should be passed on via a ViewBag or ViewData.
A SelectList should NOT be done on session as storing it is a big waste of memory. It will just linger there in memory long after the user has received his HttpResponse. A select list is just a list of options in the combo box, and as such it is not serializable (although it's content might be). You should really make the distinction between "the data inhabiting the combo box" (thin object) and "the combo box itself" (fat object). In this example - between the countries (which are an IEnumerable), and the UI element. Serializing a UI element doesn't make sense, and should always be avoided.
I have a session variable that i create dynamically, so lets say for instance i have the following session variables
Session["area1"]
Session["area2"]
Session["area3"]
Session["area4"]
And in each of those sessions i have a List in there
then i have this in order to get what the name will be and use it in my code
string areaName = "area" + Session["area"];
Session["area"] is a session variable that increases it self based on how many times a button is clicked
Now if i try to pass areaName to a function that is requiring a List as a parameter it doesn't let me do it, even though the value in that session variable is a list
I am only using "areaName" to be able to get the name of the session
How can i use it in order to pass it to function that is requiring a List type?
If you know it contains a list, then do the cast. For instance
myMethod(Session["area1"] as List<string>);
By calling the following:
string areaName = "area" + Session["area"];
You are saying, take the string "area", and append to that the variable from Session (which is an object by default), which will call it's ToString() method. Your end result here, is a string, not a list. (And not a meaningful one at that).
All you need to do is take the Session value, and cast it to the type that you know it is.
method((List<string>)Session["area"])
or
method(Session["area"] as List<string>)
The reason you can't just pass in Session["area"] without first casting, is because session returns your variable as an object. Sure, it may be a list in memory, but the system treats it the same as any other object (Look up Polymorphism for more info), which is not the List that it expects. Thus, you will get a compile time error, unless you cast it to the correct type.
I have an object of type List<SPSection>, the SPSection is a custom class that contains a string and a List<SPListItem>, SPListItem is a SharePoint object representing an item.
I want to store this in a ViewState, but I don't know how to do this. Is there a way to serialize this or convert it to some binary string, so that I could put it in a ViewState. Also when getting the value back from the ViewState, how can I convert it back to List<SPSection>.
Thanks
Don't do this. SPListItem instances cannot be retained between requests. They depend on the respective SPList instance, which in turn depends on SPWeb + SPSite, both instantiated automatically and provided through SPContext.
What you can do is retain item IDs between requests. Your custom classes need to be binary serializable, i.e. marked with the [Serializable] interface. You then just store your object under a given key into the view state: ViewState["MyObjects"] = myObjects;.
If an object is created inside a function and the function returns that type of oject how is the memory handled.
Example:
Public Function GetEmployee(employeeid as integer) as employee
Dim oEmployee as new employee
oEmployee.FirstName="Bob"
...
...
return oEmployee
end function
Does the variable that receive the object still a pointer to the memory location that was used inside the function?
What about when you do a oEmployee2=oEmployee
Is oEmployee2 just a pointer? And any changes to oEmployee will now affect the other. Just trying to understand it from a memory perspective and how that scope works
Thanks
Assuming employee is a reference type (e.g. any class) the method will return a reference (similar in concept to a pointer in unmanaged languages) to the object instance (usually on the heap). Since only one object instance exists, all changes to it will affect the instance.
If employee is a value type (e.g any struct or primitive type) a separate copy of the instance is returned.
Assuming oEmployee is a reference type (not a struct), if you pass it as an argument, then you are passing the reference. In .NET you should think in terms of Reference types vs Value types.
This article really helped me understand how memory is allocated when I was starting out.
http://www.c-sharpcorner.com/UploadFile/rmcochran/csharp_memory01122006130034PM/csharp_memory.aspx
I'm using a compiled .dll provided by someone else -- I know little about it, other than it has a session variable that I must access in a way that is strange to me. Not sure what to call it -- have googled for words that I thought might be right, but so far no success. Here is what it looks like:
Session("receipt").username
It's the .username part that I don't understand. What is it? How is it created?
Thanks for any help.
Session is probably a global object which has a default property which returns a SessionItem object. The SessionItem object is loaded from the browser-session (probably) by the Session object. The SessionItem object has a property username, which is a value stored somewhere in the browser-session.
Some code to clear things up:
Public Class Session
Private Items As SessionItemCollection
Default Public ReadOnly Property SessionItem(ByVal id As String) As Object
Get
Return Me.Items.Find(id)
End Get
End Property
End Class
And you calling the code (Which searches for the "receipt" item in the SessionItemCollection Items from Session):
Session("receipt")
My first guess (since there isn't much other code to go off of) is that the object being stored in the session variable and accessed via Session("receipt") is of a class that contains a property or member called username which you are accessing in that fashion.
The basic idea is that Session("receipt") will pull back whatever this object is (for the sake of instruction we will say it is a receipt object) and the .username is referencing the username property of that receipt object.