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.
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;
}
}
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;
}
}
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());
Member
22 Points
33 Posts
The name 'xxxx' does not exist in current context... PLEASE help me!
Aug 24, 2017 03:07 PM|sipi.perez@gmail.com|LINK
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.
and the C# behind with my class
Member
50 Points
27 Posts
Re: The name 'xxxx' does not exist in current context... PLEASE help me!
Aug 24, 2017 04:58 PM|DevNoose|LINK
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
All-Star
50841 Points
9895 Posts
Re: The name 'xxxx' does not exist in current context... PLEASE help me!
Aug 24, 2017 05:13 PM|A2H|LINK
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
Then in your page code behind assign the value to label
Aje
My Blog | Dotnet Funda