locked
Session.Add overwriting variable RRS feed

  • Question

  • User828781522 posted

    Is there something similar to Session.Add() that doesn't overwrite the var if it's already used?

    I have

    Session.Add("UserSongs", myList);


     and I need to just add an element to it each time instead of overwriting. Basically like a normal list would but that isn't lost after we leave action method. Is there something that does this?

    Sunday, September 6, 2015 11:02 AM

Answers

  • User753101303 posted

    Hi,

    Seems a confusion between "adding" a new session variable name with the corresponding value or adding a new entry to this particular value. For now my guess is that myList is a new list rather than taking its source from the existing UserSongs session variable? So instead it would be something such as:

               List<string> myList = (List<string>)Session["UserSongs"]; // Take the value from the current session variable
                if (myList == null) // Not yet stored in session, create a new list and store it as a session variable
                {
                    myList = new List<string>();
                    Session.Add("UserSongs", myList);
                }
                myList.Add(myList.Count().ToString()); // Add a new entry

    I'm just adding a new entry which is the number of elements for demonstration purpose. You could expose thiis using a strongly typed class ie your code would be just something such as :

    App.Session.UserSongs.Add("New value");

    Behind the scene, this class ensure the session variable exists etc...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 6, 2015 12:31 PM

All replies

  • User753101303 posted

    Hi,

    Seems a confusion between "adding" a new session variable name with the corresponding value or adding a new entry to this particular value. For now my guess is that myList is a new list rather than taking its source from the existing UserSongs session variable? So instead it would be something such as:

               List<string> myList = (List<string>)Session["UserSongs"]; // Take the value from the current session variable
                if (myList == null) // Not yet stored in session, create a new list and store it as a session variable
                {
                    myList = new List<string>();
                    Session.Add("UserSongs", myList);
                }
                myList.Add(myList.Count().ToString()); // Add a new entry

    I'm just adding a new entry which is the number of elements for demonstration purpose. You could expose thiis using a strongly typed class ie your code would be just something such as :

    App.Session.UserSongs.Add("New value");

    Behind the scene, this class ensure the session variable exists etc...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, September 6, 2015 12:31 PM
  • User828781522 posted

    Brilliant explanation. Thank you.

    Sunday, September 6, 2015 12:57 PM