Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8852417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:23:44+00:00 2026-06-14T13:23:44+00:00

When I put an instance of a custom class into Session and then pull

  • 0

When I put an instance of a custom class into Session and then pull it out, I need it to come out as a COPY of what’s in Session, not a reference to what’s in Session. Here’s what I have, watered down for example purposes.

protected void btnSubmit_Click(object sender, EventArgs e)
{
    Company selectedCompany = new Company("1234"); //a company code
    selectedCompany.AnotherClass.Value1 = "hello";
    Session["OLD.Company"] = selectedCompany;

    Company newCompany = (Company)Session["OLD.Company"]; //I want this to be a COPY of what's in Session, not a reference to it.
    newCompany.AnotherClass.Value1 = "goodbye";
    Session["NEW.Company"] = newCompany;
}

I stepped through and watched the Session variables, and the above code results in the AnotherClass.Value1 for BOTH OLD.Company and NEW.Company being set to “goodbye.”

Initial Google searches point me in the direction of implementing IClonable on my Company class. I tried the following, but to no avail:

public class Company : ICloneable
{
    //properties...
    //constructors...
    public object Clone()
    {
        return this.MemberwiseClone();
    }
}

and then…

protected void btnSubmit_Click(object sender, EventArgs e)
{
    Company oldCompany = (Company)Session["OLD.Company"];
    Company newCompany = (Company)oldCompany.Clone();
    newCompany.AnotherClass.Value1 = "goodbye";
    Session["NEW.Company"] = newCompany;
}

still results in Value1 for BOTH OLD.Company and NEW.Company being “goodbye”. Now I suspect this is because MemberwiseClone() creates a “shallow” copy, and my problem here is that Value1 is a value in a property which is a reference type (AnotherClass).

But at the same time, I also found this site which says DO NOT implement ICloneable. So for my purpose here I’m not really sure what to do/what advice to pursue.

Several other sites I found show some version of this:

public static object CloneObject(object obj)
{
    using (MemoryStream memStream = new MemoryStream())
    {
        BinaryFormatter binaryFormatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
        binaryFormatter.Serialize(memStream, obj);
        memStream.Seek(0, SeekOrigin.Begin);
        return binaryFormatter.Deserialize(memStream);
    }
}

This requires that I make my class serializable — which is probably okay (I’ll have to read up on Serialization), but after reading the article about not using ICloneable, I’m not sure if I should invest time in pursuing an ICloneable solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:23:46+00:00Added an answer on June 14, 2026 at 1:23 pm

    Your problem has nothing to do with Session objects. You just need to make a copy of an object right?

    Here’s how to write a copy constructor:

    http://msdn.microsoft.com/en-US/library/ms173116%28v=VS.80%29.aspx

    class Company 
    {
    ...
      public Company (Company other)
      { 
        // copy fields here....
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a custom Attribute here named AAtribute, and for instance a class
I've put an instance of the client proxy for the WCF service into a
Can we put instances of System.Activities.Activity class (WF 4.0) into the Windows Azure Cache?
I need to create my own UIView class and it is not something I
I have a simple custom object (Ingredient) with instance variables, class methods, and instance
I need a custom __reverse__ function for my class that I am deploying on
Is it possible to put an instance of android.location.Address with Intent to another activity?
I'm having an error using a custom class that inherits from LruCache in Android.
I created a ListView with a custom array adapter of MyObject . One instance
Here is what i want to do.. I have a class called userInfo. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.