Assignment: Copy vs. Reference

J

Jonathan Wood

I thought I had this figured out but I'm running into trouble.

In the code below, I update the Quantity property of a CartItem, which is
stored in the Session object. However, in another page when I read this data
back, the change is no longer reflected.

As I understand it, all my assignments are simply copying the pointer to the
actual data. But this code behaves as though I'm copying the data. Can
anyone explain why this might be? Could it have anything to do with the
Serializable attribute?

// In CS file:

[Serializable()]
public class CartItem
{
public int ProductID { get; set; }
public int Version { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
};

// In ASPX file:

public List<CartItem> CartItems
{
get { return (List<CartItem>)ViewState["CartItems"]; }
set { ViewState["CartItems"] = value; }
}

// ...
CartItem ci = CartItems.Find(delegate(CartItem item) { return
item.ProductID == id; });
if (ci != null)
{
if (e.CommandName == "Update")
{
string s = String.Format("txtQuantity{0}", id);
TextBox tb =
(TextBox)tblItems.FindControl(String.Format("txtQuantity{0}", id));
if (tb != null)
{
int qty;
if (int.TryParse(tb.Text, out qty))
{
if (qty > 0)
ci.Quantity = qty;
}
}
}
}

Thanks.

Jonathan
 
G

George

You are a bit cintradicting yourself.

you saying that you keep CarItem in Session but then
have this line
get { return (List<CartItem>)ViewState["CartItems"]; }

which creates the whole List of CartItems from ViewState.

George.
 
J

Jonathan Wood

Well, there is no contradiction as I need to store it in both ViewState and
Session. And my problem is that both places weren't matching.

However, after sleeping on this, this may be related to the problem. After a
page is posted back and the list is created from ViewState, I guess it would
then be a copy (since viewstate stores the data and not a reference to the
original object).

I need to see if this is it. Thanks.

Jonathan

George said:
You are a bit cintradicting yourself.

you saying that you keep CarItem in Session but then
have this line
get { return (List<CartItem>)ViewState["CartItems"]; }

which creates the whole List of CartItems from ViewState.

George.

Jonathan Wood said:
I thought I had this figured out but I'm running into trouble.

In the code below, I update the Quantity property of a CartItem, which is
stored in the Session object. However, in another page when I read this
data back, the change is no longer reflected.

As I understand it, all my assignments are simply copying the pointer to
the actual data. But this code behaves as though I'm copying the data.
Can anyone explain why this might be? Could it have anything to do with
the Serializable attribute?

// In CS file:

[Serializable()]
public class CartItem
{
public int ProductID { get; set; }
public int Version { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
};

// In ASPX file:

public List<CartItem> CartItems
{
get { return (List<CartItem>)ViewState["CartItems"]; }
set { ViewState["CartItems"] = value; }
}

// ...
CartItem ci = CartItems.Find(delegate(CartItem item) { return
item.ProductID == id; });
if (ci != null)
{
if (e.CommandName == "Update")
{
string s = String.Format("txtQuantity{0}", id);
TextBox tb =
(TextBox)tblItems.FindControl(String.Format("txtQuantity{0}", id));
if (tb != null)
{
int qty;
if (int.TryParse(tb.Text, out qty))
{
if (qty > 0)
ci.Quantity = qty;
}
}
}
}

Thanks.

Jonathan
 
G

George

You got it....

that is pretty much what I meant by my post.
It's just nowhere I saw that you using the Session object with CarItems


George.

Jonathan Wood said:
Well, there is no contradiction as I need to store it in both ViewState
and Session. And my problem is that both places weren't matching.

However, after sleeping on this, this may be related to the problem. After
a page is posted back and the list is created from ViewState, I guess it
would then be a copy (since viewstate stores the data and not a reference
to the original object).

I need to see if this is it. Thanks.

Jonathan

George said:
You are a bit cintradicting yourself.

you saying that you keep CarItem in Session but then
have this line
get { return (List<CartItem>)ViewState["CartItems"]; }

which creates the whole List of CartItems from ViewState.

George.

Jonathan Wood said:
I thought I had this figured out but I'm running into trouble.

In the code below, I update the Quantity property of a CartItem, which
is stored in the Session object. However, in another page when I read
this data back, the change is no longer reflected.

As I understand it, all my assignments are simply copying the pointer to
the actual data. But this code behaves as though I'm copying the data.
Can anyone explain why this might be? Could it have anything to do with
the Serializable attribute?

// In CS file:

[Serializable()]
public class CartItem
{
public int ProductID { get; set; }
public int Version { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
};

// In ASPX file:

public List<CartItem> CartItems
{
get { return (List<CartItem>)ViewState["CartItems"]; }
set { ViewState["CartItems"] = value; }
}

// ...
CartItem ci = CartItems.Find(delegate(CartItem item) { return
item.ProductID == id; });
if (ci != null)
{
if (e.CommandName == "Update")
{
string s = String.Format("txtQuantity{0}", id);
TextBox tb =
(TextBox)tblItems.FindControl(String.Format("txtQuantity{0}", id));
if (tb != null)
{
int qty;
if (int.TryParse(tb.Text, out qty))
{
if (qty > 0)
ci.Quantity = qty;
}
}
}
}

Thanks.

Jonathan
 
J

Jonathan Wood

Yeah, I did think about this issue overnight. But I understood what you
meant in your post and it helped keep me pointed in the right direction.

Thanks.

Jonathan

George said:
You got it....

that is pretty much what I meant by my post.
It's just nowhere I saw that you using the Session object with CarItems


George.

Jonathan Wood said:
Well, there is no contradiction as I need to store it in both ViewState
and Session. And my problem is that both places weren't matching.

However, after sleeping on this, this may be related to the problem.
After a page is posted back and the list is created from ViewState, I
guess it would then be a copy (since viewstate stores the data and not a
reference to the original object).

I need to see if this is it. Thanks.

Jonathan

George said:
You are a bit cintradicting yourself.

you saying that you keep CarItem in Session but then
have this line
get { return (List<CartItem>)ViewState["CartItems"]; }

which creates the whole List of CartItems from ViewState.

George.

I thought I had this figured out but I'm running into trouble.

In the code below, I update the Quantity property of a CartItem, which
is stored in the Session object. However, in another page when I read
this data back, the change is no longer reflected.

As I understand it, all my assignments are simply copying the pointer
to the actual data. But this code behaves as though I'm copying the
data. Can anyone explain why this might be? Could it have anything to
do with the Serializable attribute?

// In CS file:

[Serializable()]
public class CartItem
{
public int ProductID { get; set; }
public int Version { get; set; }
public string Title { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
};

// In ASPX file:

public List<CartItem> CartItems
{
get { return (List<CartItem>)ViewState["CartItems"]; }
set { ViewState["CartItems"] = value; }
}

// ...
CartItem ci = CartItems.Find(delegate(CartItem item) { return
item.ProductID == id; });
if (ci != null)
{
if (e.CommandName == "Update")
{
string s = String.Format("txtQuantity{0}", id);
TextBox tb =
(TextBox)tblItems.FindControl(String.Format("txtQuantity{0}", id));
if (tb != null)
{
int qty;
if (int.TryParse(tb.Text, out qty))
{
if (qty > 0)
ci.Quantity = qty;
}
}
}
}

Thanks.

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top