Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6682305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:41:26+00:00 2026-05-26T04:41:26+00:00

This is minor, I know, but let’s say that I have a class Character

  • 0

This is minor, I know, but let’s say that I have a class Character and a class Ability (mostly because that’s what I’m working on). Class Character has six abilities (so typical D&D…). basically:

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 actually using these ability properties in future code, I’d like to be able to default to just the score, like so:

//Conan hits someone
int damage = RollDice("2d6") + Conan.Str;

//evil sorcerer attack drains strength
Conan.Str = 0;

rather than:

//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 anybody help me with the reverse? Implicit conversion like this:

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

will replace the entire attribute rather than just the score of the attribute—not the desired result…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:41:27+00:00Added an answer on May 26, 2026 at 4:41 am

    First, keep your implicit conversion:

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

    Then in your character class: Add a private Ability attribute for str, and change the getter and the 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 could also use:

                    if(string.IsNullOrWhiteSpace(value.Name))
    

    instead of

                    if (value.Name == "")
    

    If you are compiling to .NET 4.0 version

    EDIT: I gave you a solution that does exactly what you wanted to, but What ja72 wrote is also a good suggestion with operators + and -; you can add his solution to mine (or mine to him, whatever), it will work just fine. You will then be able to 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The page works fine in Chrome, but I have this one minor error in
Let's say that you have a product that is written in Visual Studio and
I know this is a minor issue, but why, if you use scaffolding in
Say I'm working on a project and I know that I've completed a specific
This is a minor style question, but every bit of readability you add to
This is one of the minor CSS problems that plague me constantly. How do
I understand that Microsoft uses this template when versioning their products: Major.Minor.Build.Revision. Major is
Okay, this is just a minor caveat. I am currently working with the lovely
I've got a ListView that works just great, except for this minor annoyance. I
I know it's a minor annoyance.. but it's still an annoyance and it baffles

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.