Using session to get/set object properties everytime

asp.net, c#, session

Solution

It looks like you're pretty close, but you don't have anything to actually store the object in session. Try something like this:

public static Example Instance
{
    get
    {
        //store the object in session if not already stored
        if (Session["example"] == null)
            Session["example"] = new Example();

        //return the object from session
        return (Example)Session["example"];
    }
}

This is basically just a web-friendly implementation of the Singleton Pattern.

Problem

I tried searching for this but I'm not even sure how to phrase it for the search. What I'm attempting to do is have a class that everytime I access it to change it, I'm really getting and setting the value from session. Here's what I'm trying to do (what I have so far.): ``` public class example { public int prop1 {get;set;} public static example Instance { return (example)(HttpContext.Current.Session["exampleClass"] ?? new example()); } } public class main { protected void Page_Load(object sender, EventArgs e) { example.Instance.prop1 = "aaa"; //stores value into session txtInput.Text = example.Instance.prop1; //retrieves value from session } } ``` I hope that makes sense on what I am trying to do. Any help would be appreciated, thank you.

Original source