1

I'm trying to do this:

   Data4JS D4JS = (Data4JS)GlobalHelper.GetCurrentSession()["Data4JS"];
    GlobalHelper.GetCurrentSession()["Data4JS"] = null; 
    return D4JS;

Problem is it sets D4JS to null as well, I don't really want to split my code up into a few method calls, how else can I acheive this easily?

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
  • You need to clone object. try to search about objects cloning/copying. – Renatas M. Aug 14 '12 at 09:09
  • 1
    If `D4JS` is not null after the first line, there's no reason it should be null after the second line. Go and try it with any associative collection (`Dictionary` for example). Something else happens then and your snippet is missing these important details. My advice is that you debug this twice, figure out what happens and edit your question. – Wiktor Zychla Aug 14 '12 at 09:12

3 Answers3

2

just use the new keyword so you wont use a reference, or clone object method.

Deep cloning objects

Community
  • 1
  • 1
Freeman
  • 5,691
  • 3
  • 29
  • 41
  • How do I do the new bit? That's nto working for me,... Data4JS D4JS = new Data4JS(); D4JS = (Data4JS)GlobalHelper.GetCurrentSession()["Data4JS"]; – William Sando Aug 14 '12 at 09:53
  • Monkieboy gave a pretty good ideea of how to make a new object, but as i said you can use clone for a cleaner approach. – Freeman Aug 14 '12 at 10:08
0

In that case do a deep copy using serialization.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
0

Assuming your object is simple enough and has public properties and all you need are the values in the property you could do something similar to this:

Data4JS D4JS = (Data4JS)GlobalHelper.GetCurrentSession()["Data4JS"];
Data4JS result = new Data4JS
{
    //Set your result object to the one in 
    //session by manually copying over the property values
    Property1 = D4JS.Property1;
};

GlobalHelper.GetCurrentSession()["Data4JS"] = null; 

return result;

Otherwise use what Freeman has suggested as a cleaner way of achieving what you need.

Mr. Mr.
  • 4,257
  • 3
  • 27
  • 42