How to create a simplified assignment or default property for a class in C #

This is not enough, I know, but let me say that I have a cool character and class ability (mainly because I'm working on it). The class character has six abilities (typical D & D ...). primarily:

public class Character { public Character() { this.Str = new Ability("Strength", "Str"); this.Dex = new Ability("Dexterity", "Dex"); this.Con = new Ability("Constitution", "Con"); this.Int = new Ability("Intelligence", "Int"); this.Wis = new Ability("Wisdom", "Wis"); this.Cha = new Ability("Charisma", "Cha"); } #region Abilities public Ability Str { get; set; } public Ability Dex { get; set; } public Ability Con { get; set; } public Ability Int { get; set; } public Ability Wis { get; set; } public Ability Cha { get; set; } #endregion } 

and

 public class Ability { public Ability() { Score = 10; } public Ability(string Name, string Abbr) : this() { this.Name = Name; this.Abbr = Abbr; } public string Name { get; set; } public string Abbr { get; set; } public int Score { get; set; } public int Mod { get { return (Score - 10) / 2; } } } 

When you really use these property properties in future code, I would like to be able to use only the default account:

 //Conan hits someone int damage = RollDice("2d6") + Conan.Str; //evil sorcerer attack drains strength Conan.Str = 0; 

but not:

 //Conan hits someone int damage = RollDie("2d6") + Conan.Str.Score; //evil sorcerer attack drains strength Conan.Str.Score = 0; 

Now the first case can be taken care of with an implicit conversion:

 public static implicit operator int(Ability a) { return a.Score; } 

Can someone help me with the opposite? Implicit conversion as follows:

 public static implicit operator Ability(int a) { return new Ability(){ Score = a }; } 

will replace the entire attribute, not just the attribute of the attribute, not the desired result ...

+6
source share
4 answers

First save your implicit conversion:

 public static implicit operator Ability(int a) { return new Ability(){ Score = a }; } 

Then in your character class: add the private ability attribute for str and change the getter and setter of the Str property as follows:

  private Ability str; public Ability Str { get { return this.str; } set { if (value.Name == "") { this.str.Score = value.Score; } else { this.str = value; } } } 

There you go :)

You can also use:

  if(string.IsNullOrWhiteSpace(value.Name)) 

instead

  if (value.Name == "") 

If you are compiling .NET 4.0

EDIT . I gave you a solution in which exactly what you wanted , but what ja72 wrote is also a good suggestion with the + and - operators; you can add your decision to mine (or mine to it, whatever), it will work fine. Then you can write:

  Character Jax = new Character(); // Str.Score = 10 Character Conan = new Character(); // Str.Score = 10 Jax.Str = 2000; // Str.Score = 2000; Conan.Str += 150; // Str.Score = 160 
+3
source

The best you can do is increase your score by adding these methods to Ability .

  public static Ability operator + (Ability lhs, int score) { lhs.Score += score; return lhs; } public static Ability operator - (Ability lhs, int score) { lhs.Score -= score; return lhs; } public static implicit operator int(Ability rhs) { return rhs.Score; } 

and using them as:

  static void Main(string[] args) { Character evil = new Character(); //Str.Sccore=10 evil.Str += 10; //cast spell for Str.Sccore=20 evil.Str -= evil.Str; //death with Str.Sccore=0 } 
+4
source

Another option is to replace properties with delegates like this

 public class Character { public Character() { ... } #region Abilities ... #endregion public Func<int> Strength { get { return () => Str.Score; } set { Str.Score = value(); } } } 

and use it like this:

  Character evil = new Character(); //Str.Sccore=10 // fist spell hits evil.Strength = () => 5; //set Str.Score=5 // second spell hits evil.Strength = () => 0; //set Str.Score=5 if (evil.Strength() == 0) { // dead } 
+1
source

Perhaps you could make an abstract of Ability , and then derive new classes from Ability for each of the subclasses: Strength , ...

The constructor of the Strength class will look something like this:

 public Strength () : base ("Strength", "Str") { ...} 

Now properties of properties with Character will be strongly typed, and implicit conversions can turn a value of type 5 into a Strength object with a value of 5. This will also prevent you from accidentally saving Dexterity in the Strength property, for example.

[Assuming that the name and abbreviations are actually fixed for all objects of this type.]

0
source

Source: https://habr.com/ru/post/899176/


All Articles