I'm currently writing a web-based game in ASP.NET and C#.  I'm in the
process of testing whether or not I successfully hooked my attack
methods into .NET's event model.  I need both the user-controlled
character and the computer-controlled enemy to retain their state
between turns, so I can see if the attack is actually working.  My
problem is that the compiler is choking on the part where I try to
retrieve my objects from the session.  Specifically, it says that
those objects don't exist, despite my explicit casts to the proper
types, and the fact that the first time the page is accessed, new
objects are made.  My code is below:

//Note: mainLabel is a Label server control, and attack is a simple
Button control

using System;
using System.IO;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            PlayerCharacter PC = (PlayerCharacter)(Session["player"]);
            PlayerCharacter Enemy = (PlayerCharacter)(Session
["enemy"]);
        }
        else
        {
            PlayerCharacter PC = new PlayerCharacter("Neo", "Male", "A
young hacker, unaware that he's the chosen one", new Hacker());
            PlayerCharacter Enemy = new PlayerCharacter("Smith",
"Male", "An agent of the Matrix", new Shill());
        }

        /* from this point on, neither PC nor Enemy are recognized by
the compiler, so it gives me a bunch of errors */

        AttackEventArgs attackArgs = new AttackEventArgs(PC, Enemy);

        mainLabel.Text = PC.Name + " ";
        mainLabel.Text += PC.Gender + "\n<br />";
        mainLabel.Text += "HP: " + PC.CurrentHP + " TP: " +
PC.CurrentTP + " DMG: " + PC.DMG + "\n<br />";
        PC.Money = 2.34f;
        mainLabel.Text += "Money: $" + PC.Money + "\n<br />";
        mainLabel.Text += PC.Class.GetType().ToString() + "\n<br /
><br />";
        mainLabel.Text += "Attack info before leveling up: <br />";
        mainLabel.Text += "DMG: " + PC.DMG + " DMG Modifier " +
PC.DMGModifier + " Chance to Hit: " + PC.ChanceToHit + "<br /><br />";

        PC.LevelUp();

        mainLabel.Text += "Attack info after leveling up: <br />";
        mainLabel.Text += "DMG: " + PC.DMG + " DMG Modifier " +
PC.DMGModifier + " Chance to Hit: " + PC.ChanceToHit + "<br /><br />";

        List<Attack> PCattacks = PC.Attacks;

        mainLabel.Text += "Enemy HP: " + Enemy.CurrentHP + "<br /><br /
>";

        attack.Click += delegate { PCattacks[0].ExecuteAttack(this,
attackArgs); Session["player"] = PC; Session["enemy"] = Enemy; };
    }
}

Reply via email to