RE: Session variables and reference



Vince13,

drop a Gridview on an ASP.NET page and put the following code in. This
should help explain what's happening with Session:

protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("Test");
DataRow row = dt.NewRow();
row["Test"] = "First";
dt.Rows.Add(row);
Session["dt"]=dt;
dt.Rows[0]["Test"] = "Second";
GridView1.DataSource = (DataTable) Session["dt"];
GridView1.DataBind();
}

-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com



"Vince13 via DotNetMonster.com" wrote:

I am trying to set up a page where the user can change data, and then click
cancel and the data will not be saved. Currently, I am saving the data in a
Session variable that is an array of a class I created:

public class cut
{
public cut()
{
ParallelDim = 'G';
cutPoint = ++numCuts;
Knom = 0;
Kmin = 0;
Kmax = 0;
//numCuts++;
}


public cut(cut myCut) //copy constructor
//**note, does NOT increment numCuts
{
ParallelDim = myCut.ParallelDim;
cutPoint = myCut.cutPoint;
Knom = myCut.Knom;
Kmin = myCut.Kmin;
Kmax = myCut.Kmax;
}

public char ParallelDim;
public int cutPoint;
public decimal Knom;
public decimal Kmin;
public decimal Kmax;
public static int numCuts = 0;
}

I realized that when I set my temporary array to equal the session array, it
was passing the values by reference, so I created new objects using a copy
constructor:

for(int i = 0; i < 20; ++i) //uses copy constructor to initiate myCuts and
make copy so the
{ //changes are not made to Session[allcuts]
myCuts[i] = new cut(((cut[])Session["allCuts"])[i]);
//uses copy constructor to make
identical array
}

This works in that it does create a new array that is not just a reference to
the old one, but for some reason I cannot write the data back to the session
array. I am fairly sure it should just work like this:

Session["allCuts"] = myCuts;

but I have also tried using the copy constructor again (reverse of above) and
setting each individual value of the class separately. Nothing will write
the data back to the session array.

If ANYONE has any ideas, I would greatly appriciate it.

--
Message posted via http://www.dotnetmonster.com


.



Relevant Pages

  • Re: Session variables and reference
    ... If you are going the session route, I find it easier to store ina data ... applications. ... public cut//copy constructor ... I realized that when I set my temporary array to equal the session array, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: help with array within another array
    ... you could make a hash with reference to hash values: ... it seems like you might want to consider using the node identifier as the first key in %SESSION, and make the value a reference to hash keyed by the owner and severity: ... I am trying to declare two associative array ...
    (perl.beginners)
  • Re: [PHP] SESSION problem
    ... But, if you came from a background that predated associate arrays, then using numeric indexes would seem more natural. ... looked into the session extension in that level of detail, but I doubt such a limitation would exist if there was not a very good reason for it. ... Given that I can't see into the future, even if I currently only have one set of data to store in the session I wouldn't just chuck the bits of paper from the file into the filing cabinet. ... The root level of the session array should contain descriptive keys, and I've never been in a situation where 5318008 is descriptive and gives context to the data unless you turn it upside down! ...
    (php.general)
  • Re: 2 dimension arrays
    ... > How do I declare an array as a session variable? ... as you have a *reference* to this array, there is really only one array ... >> Andrew wrote: ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Using The Same Page To Display A Hundred Pictures One Picture At A Time
    ... >> a temporary cache of the array quite easily. ... As far as I can tell session variables are just little text files ... That way the script can just load it from ... there even for the first load per session. ...
    (comp.lang.php)