Reputation: 297
I am new to C# and I got a question about how to subtract a number from a label?
I am trying to make an application. The situation is as followed:
I got a button that says "attack" and there is a label called "lblTotalEnemyLifes". Now when I press the button I want to subtract 1 "damage" from the total hp that the label has and the label would automatically be updated after I press the button. I tried alot of different things and nothing seems to work. At the moment I tried things like:
int damage = -1;
int life = Convert.ToInt32(lblTotalEnemyLifes.Text);
life = life - damage;
I tried alot more then this. If someone could help me how I subtract a number from a label.. that would be great.
Thanks in advance!
Upvotes: 1
Views: 2825
Reputation: 112537
It is a bad idea to store your data in labels. Labels and textboxes should only be used for user interaction (input and output/display). Store your data in variables, fields or properties. It is even better to use separate classes for your game logic instead of performing the logic inside the form
public class Game
{
private const int InitialLifesCount = 100;
public Game()
{
EnemyLifes = InitialLifesCount;
}
public int EnemyLifes { get; set; }
public AttackEnemy()
{
EnemyLifes--;
}
}
In the form you can write
private _game = new Game();
In the button click:
_game.AttackEnemy();
lblTotalEnemyLifes.Text = _game.EnemyLifes.ToString();
It is also a good idea to have a look at data binding. If you implement the INotifyPropertyChanged
interface in your game class and bind the class properties to your labels, then the labels will update automatically.
Besides enhancing clarity, the separation of logic and I/O allows you to write unit tests for your game logic. In a unit test you can write
[TestMethod]
public void AttackingEnemyReducesHisLifes()
{
var game = new Game();
int lifesBefore = game.EnemyLifes;
game.AttackEnemy();
Assert.AreEqual(lifesBefore - 1, game.EnemyLifes);
}
Upvotes: 1
Reputation: 101701
You are subtracting the number but don't write it back:
lblTotalEnemyLifes.Text = life.ToString();
Changing the value of life
doesn't affect your label.You should update label's Text
property.
And also you are subtracting -1
instead of 1
and the expression becomes life = life + 1
. You can just use the decrement
operator to substract 1
from your variable:
int life = Convert.ToInt32(lblTotalEnemyLifes.Text);
life--; // equivelant to life = life - 1;
lblTotalEnemyLifes.Text = life.ToString();
Upvotes: 3
Reputation: 1681
another approach, possible more elegant and less error prone, is this:
wrapp your value in a class:
public class GameState
{
public int TotalEnemyLifes { get; set; }
}
next, instantiate the class and bind it to the UI:
var ctx = new GameState { TotalEnemyLifes = LIFE_VALUE };
this.DataContext = ctx;
next, update the value in your button event handler:
private void event_Handler(object sender, RoutedEventArgs e)
{
ctx.TotalEnemyLifes--;
BindingOperations.GetBindingExpression(lblTotalEnemyLifes, Label.ContentProperty).UpdateTarget();
}
Upvotes: 2
Reputation: 715
Try to set damage to 1, not -1. Then add at the end:
lblTotalEnemyLifes.Text = life.ToString();
Upvotes: 1