Session variables and reference

  • Thread starter Thread starter Vince13 via DotNetMonster.com
  • Start date Start date
V

Vince13 via DotNetMonster.com

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 = new cut(((cut[])Session["allCuts"]));
//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.
 
If you are going the session route, I find it easier to store ina data
format, like a DataSet. This is not applicable for all.

If you are using a single page for the info, consider ViewState instead. You
are still better to control your own destiny, of course.

I have never had problems with objects in Session, but I am not extensive in
my use of Session either, as I can normally find a better way for my
applications.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
Co-author: Microsoft Expression Web Bible (upcoming)

************************************************
Think outside the box!
************************************************
 
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
 
I see what you're pointing out here, that the assignment operator merely
creates another reference to the same memory, but that's why I'm using the
copy contructor in the first place. That's also the part that works. What
I'm wondering is if the same issue (reference/value) is somehow not allowing
me to wirte back to the session variable.
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
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
[quoted text clipped - 54 lines]
If ANYONE has any ideas, I would greatly appriciate it.
 
Back
Top