locked
The name 'xxxx' does not exist in current context... PLEASE help me! RRS feed

  • Question

  • User-121299044 posted

    I'm sorry to bother but I need some help from the experts!  I'm trying to do an exercise that will create a simple class called Character, with 3 properties and 2 methods... on the page side, I have only a single Label called "resultLabel"... the problem is that I want to assign some text to this server control from this new class, and I get an error message saying that the name "resultLabel" does not exist in current context... I can't access my server label from other class than _Default ?? Please advise... Below is my code.

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"   %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server"></head>
    <body>
        <form id="miforma" runat="server">
            <asp:Label ID="resultLabel" runat="server" Text=""></asp:Label>        
        </form>
    </body>
    </html>
    

    and the C# behind with my class

    using System;
    
    public partial class _Default : System.Web.UI.Page
    {
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
            Character Hero = new Character() { Name="Hulk", Health = 100, DamageMax = 25, AttackBonus = 5 };
            Character Monster = new Character() { Name = "Loki", Health = 100, DamageMax = 25, AttackBonus = 5 };
    
            Monster.Defend(Hero.Attack());
    
        }
    }
    
    public class Character
    {
    
        private Random myRandom = new Random();
    
        public string Name { get; set; }
        public int Health { get; set; }
        public int DamageMax { get; set; }
        public int AttackBonus { get; set; }
    
        public int Attack() {
            return myRandom.Next(1, 35);
        }
    
        public void Defend(int damage) {
            resultLabel.Text = $"{Name} suffered {damage}% damage..."; <<<<============== ERROR! ERROR! ERROR!
            this.Health -= damage;
        }
    
    }

    Thursday, August 24, 2017 3:07 PM

All replies

  • User464517482 posted

    Hi, 

    you get the error because resultLabel is not member of Character and it is not accessible from there.

    I am kindly advise you not to mix the logic, if i were you i would add a public property DefendResult in Characters, in Defends method i would set the value of DefendResult with your message, and i would fill the labelResult text in the Page_Load event something like this

    using System;
    
    public partial class _Default : System.Web.UI.Page
    {
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
            Character Hero = new Character() { Name="Hulk", Health = 100, DamageMax = 25, AttackBonus = 5 };
            Character Monster = new Character() { Name = "Loki", Health = 100, DamageMax = 25, AttackBonus = 5 };
    
            Monster.Defend(Hero.Attack());
            resultLabel.Text = Monster.DemageResult
    
        }
    }
    
    public class Character
    {
    
        private Random myRandom = new Random();
    
        public string Name { get; set; }
        public int Health { get; set; }
        public int DamageMax { get; set; }
        public int AttackBonus { get; set; }
        public string DamageResult { get; set; }
    
        public int Attack() {
            return myRandom.Next(1, 35);
        }
    
        public void Defend(int damage) {
            this.DamageResult = $"{Name} suffered {damage}% damage..."; <<<<============== ERROR! ERROR! ERROR!
            this.Health -= damage;
        }
    }
    Thursday, August 24, 2017 4:58 PM
  • User2103319870 posted

    As DevNoose mentioned you cannot access label control from Character class. An alternative approach is to make the Defend method return formatted string.

    Sample Implemenation

    public class Character
        {
    
            private Random myRandom = new Random();
    
            public string Name { get; set; }
            public int Health { get; set; }
            public int DamageMax { get; set; }
            public int AttackBonus { get; set; }
            public string DamageResult { get; set; }
    
            public int Attack()
            {
                return myRandom.Next(1, 35);
            }
            //Change Void to string 
            public string Defend(int damage)
            {
                this.Health -= damage;
                //Return the formatted string
                return  $"{Name} suffered {damage}% damage..."; //<<<<============== ERROR!ERROR!ERROR!
            }
        }

    Then in your page code behind assign the value to label

     Character Hero = new Character() { Name = "Hulk", Health = 100, DamageMax = 25, AttackBonus = 5 };
                Character Monster = new Character() { Name = "Loki", Health = 100, DamageMax = 25, AttackBonus = 5 };
    
    
                resultLabel.Text = Monster.Defend(Hero.Attack());

    Thursday, August 24, 2017 5:13 PM