Reputation: 87
I'm very new to ASP.NET so there is a high risk that I am missing something simple here, but I have a problem with my Session. When I click the "NewTurn" button the first time, it creates the player just as it should, however, when I click it again, somehow all the stats are back as if it got created again (stats such as "health" is back to max value) but I can see in debug mode that it only enters the "if == null" function ONCE.
I don't understand what I am doing wrong, here is the code:
protected void NewTurn_Click(object sender, ImageClickEventArgs e)
{
StartButton.Visible = false;
RulesLink.Visible = false;
NewTurnButton.Visible = true;
if (Session["PrevObject"] == null)
{
Character player = new Character(1);
Session["PrevObject"] = player;
}
Character prevObj = (Character)Session["PrevObject"];
prevObj = CreateCard(prevObj);
Session["PrevObject"] = prevObj;
}
EDIT: I found the error and it had nothing to do with the Session, I just returned the object without setting the correct values before sending it back. Thank you for your suggestions, though and I'm sorry to have wasted your time!
Upvotes: 0
Views: 73
Reputation: 6
You should have to mention the Property of the player will be assign to the Session["PrevObject"]. suppose I have the class such as
public class Character
{
public playtime{get;set;}
Character(int i)
{
playtime=i;
}
}
Now I will code as you mention:
protected void NewTurn_Click(object sender, ImageClickEventArgs e)
{
StartButton.Visible = false;
RulesLink.Visible = false;
NewTurnButton.Visible = true;
if (Session["PrevObject"] == null)
{
Character player = new Character(1);
//Here is a little change you will have to specify what shuold be assign to the Session //variable
Session["PrevObject"] = player.playtime;
}
Hope this will help you. Vote if you find helpful Thanks is always appreciated.
Upvotes: 0
Reputation: 11104
please check comment in your code
protected void NewTurn_Click(object sender, ImageClickEventArgs e)
{
StartButton.Visible = false;
RulesLink.Visible = false;
NewTurnButton.Visible = true;
if (Session["PrevObject"] == null)
{
Character player = new Character(1);
Session["PrevObject"] = player;
}
Character prevObj = (Character)Session["PrevObject"];
// I think here you made mistake
prevObj = CreateCard(prevObj);
// check here again using DEbug point
// and make sure prevObj not null
Session["PrevObject"] = prevObj;
}
Upvotes: 1