So i have a simple object oriented C# code which doesn’t behave as expected.
Here is my object class :
internal class Fighter
{
public int Id { get; }
public string Name { get; set; }
int Health { get; set; } = 100;
int Height { get; set; } = 180;
int Weight { get; set; } = 78;
static Weapon weapon { get; set; }
int WeaponValue { get; } = (int)weapon;
int DealedDamage { get; set; } = 0;
bool IsMale { get; set; } = true;
internal Random Random { get; private set; } = new Random();
bool IsDead { get; set; } = false;
public Fighter(int id, string name, Weapon weap) { this.Id = id; this.Name = name; weapon = weap;}
public void Description()
{
Console.WriteLine("Fighter ID : " + Id + ", Name : " + Name + ", Weapon : " + weapon + ", dealing " + WeaponValue + " pts of damage.");
}
}
My ‘Weapon’ enum :
internal enum Weapon { Couteau = 1, Machette = 3, Pistolet = 5, Grenade = 10, Mitraillette = 15, Fusil = 30 }
And finally my Program.cs class :
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
Console.WriteLine("Bienvenue dans le programme de baston.");
Fighter fighter1 = new Fighter(0, "Gaston", Weapon.Couteau);
Fighter fighter2 = new Fighter(1, "Francis", Weapon.Grenade);
fighter1.Description();
fighter2.Description();
}
}
I get this output running my code in debug mode in Visual Studio 2022 :
Hello, World!
Bienvenue dans le programme de baston.
Fighter ID : 0, Name : Gaston, Weapon : Grenade, dealing 0 pts of damage.
Fighter ID : 1, Name : Francis, Weapon : Grenade, dealing 1 pts of damage.
Sortie de C:UsersdonnaDesktopFightingTournamentbinDebugnet6.0FightingTournament.exe (processus 23112). Code : 0.
Pour fermer automatiquement la console quand le débogage s'arrête, activez Outils->Options->Débogage->Fermer automatiquement la console à l'arrêt du débogage.
Appuyez sur une touche pour fermer cette fenêtre. . .
It is not what I expected, I should have different weapons for the 2 objects I instantiated.
How is this possible ?
Thanks for reading
I tried placing the enum in its own namespace.
I tried changing visibility of the enum etc.
I also tried passing different values for Weapon variable.
Every time the output is incorrect.
2
Answers
It’s because you have marked weapon as
static
. This means all fighter classes share one weapon so all fighters will have the last weapon assigned to any fighter.Just declare
weapon
as public and it will work as expected:In order to get the weapon value from a non static property, you need a computed property:
Your
Weapon
-property isstatic
, which means all your class’ instances share the exact same object. However in your case you want to reference the property within a properties initial-value, which isn’t allowed.However even if that was allowed, you could only change the default value for your
WeaponValue
-property. Whenever you’d change yourWeapon
-property to something else, yourWeaponValue
wouldn’t change, because it isget
-only and only has an initial value set. However what you probably want instead is to change that value whenever someone changesWeapon
.So after all do this: