Reputation: 19
I have a class created:
public class character
{
public string Name, Owner;
public int Str, Con, Dex, Int, Wis, Cha, AC, Speed, maxHP, currHP, AP, SV, Surges;
}
As you can see, it's a very simple class at the moment. My question is, is there a way to create another class inside this so when I call my function I can have a math equation returned?
Example:
character c = new character();
c.Name = "Goofy";
c.Owner = "Me";
c.Str = 15;
MessageBox.Show(c.Str.Mod);
The output to the window would be "7" (Mod is: Math.Floor(Str / 2);)
I have been trying to search both SO and Google for some time and have yet to figure this out. I may be searching for the wrong phrases or this might not even be possible.
Thanks
Upvotes: 0
Views: 174
Reputation: 520
Rather than using Int fields for your stats (at least for your example), make a Stat class, like so:
public class character
{
public string Name, Owner;
public int AC, Speed, maxHP, currHP, AP, SV, Surges;
public Stat Str { get; set; }
public Stat Con { get; set; }
public Stat Dex { get; set; }
public Stat Int { get; set; }
public Stat Wis { get; set; }
public Stat Cha { get; set; }
public class Stat
{
public Stat(int stat)
{
Value = stat;
}
public int Value { get; set; }
public int Mod { get { /*Calcuate Mod from Value.*/; } }
}
}
And call it like this:
character c = new character();
c.Name = "Goofy";
c.Owner = "Me";
c.Str = new Stat(7);
MessageBox.Show(c.Str.Value); //The Value
MessageBox.Show(c.Str.Mod); //The Mod of Strength
Upvotes: 1
Reputation: 700152
To use that exact syntax, you would need Str
to be a different type. One that can be converted from an int and have a property named Mod
:
public class character {
// A type that is convertible from int and has a Mod property
public class Modder {
//private variable to hold the integer value
private int _value;
// private constructor that is used by the converter
private Modder(int value){ _value = value; }
// implicit converter to handle the conversion from int
public static implicit operator Modder(int value) {
return new Modder(value);
}
// Property Mod that does the calculation
public int Mod {
get { return _value / 2; }
}
}
public string Name, Owner;
public int Con, Dex, Int, Wis, Cha, AC, Speed, maxHP, currHP, AP, SV, Surges;
public Modder Str;
}
I put that class insde the character
class, but it doesn't have to be.
Upvotes: 0
Reputation: 2416
If Mod is a operation that could apply to any integer, you could define it as an extension method.
static class ExtendInt
{
static public int Mod(this int stat)
{
return stat / 2;
}
}
class UseExtendedInt
{
void UseIt()
{
int a = 1;
int b = a.Mod();
}
}
If you put ExtendInt inside a unique namespace, you can use that namespace only in the files where you want this to work, but otherwise this will be an available function for all integers in that namespace.
If this needs different operations depending on the variable name, you'll have to somehow incorporate reflection or define an enum that identifies the calculation type to pass into Mod.
In most cases, if the calculation types are different, you're better off defining properties like strMod, conMod, etc...
Upvotes: 0
Reputation: 151588
Create an interface. Use properties. Use descriptive variable names:
public interface ICharacter
{
public string Name { get; }
public int Strength { get; }
}
Then implement it:
public class Character : ICharacter
{
public string Name { get; private set; }
public int Strength { get; private set; }
public Character(string name, int strength)
{
this.Name = name;
this.Strength = strength;
}
}
Now for your question, you should let one class do one thing. So now you can create and initialize a calculator for a character's damage modifier (or whatever "Mod" means):
public class DamageModifierCalculator
{
public int Calculate(ICharacter character)
{
return (int)Math.Floor(character.Strength / 2);
}
}
Now initialize and call it:
var character = new Character("Goofy", 15);
var calculator = new DamageModifierCalculator();
int mod = calculator.Calculate(character);
It's extensible, it's testable and its concerns are separated. You will want to create an interface for the calculator too, as you'll need more of them, preferably one for each kind of calculation.
Or you can just stick it in your Character
class, but then it's got nothing to do with OO anymore.
Upvotes: 1
Reputation: 227
For this operation, you can just do :
int x = (int)15 / 2;
Also, you can create nested class, that is to say, a class inside a class. For example, you would have inside your class something like :
public class MathOperator
{
public int Mod(int x)
{
return (int)x/2;
}
}
And then, just create an instance of this nested class in your class, and use it on c.Str
Upvotes: 0
Reputation: 20090
only way that I can quickly think is Extension Methods
class Program
{
static void Main(string[] args)
{
character c = new character();
c.Name = "Goofy";
c.Owner = "Me";
c.Str = 15;
Console.WriteLine(c.Str.Mod());
Console.Read();
}
}
public class character
{
public string Name, Owner;
public int Str, Con, Dex, Int, Wis, Cha, AC, Speed, maxHP, currHP, AP, SV, Surges;
}
public static class Ext
{
public static int Mod(this int value)
{
return (int)Math.Floor(value / 2.0);
}
}
Upvotes: 5
Reputation: 6908
Yes, you can create a method called Mod
that will do your math for you. It'd look something like this:
public class character
{
public string Name, Owner;
public int Str, Con, Dex, Int, Wis, Cha, AC, Speed, maxHP, currHP, AP, SV, Surges;
public double Mod(int stat)
{
return Math.Floor(stat/2);
}
}
Upvotes: 1
Reputation: 3415
or:
public class character
{
public string Name, Owner;
public int Str, Con, Dex, Int, Wis, Cha, AC, Speed, maxHP, currHP, AP, SV, Surges;
public int StrMod{
get{
return (int)Math.Floor(Str / 2);
}
}
}
used with:
character c = new character();
c.Name = "Goofy";
c.Owner = "Me";
c.Str = 15;
MessageBox.Show(c.StrMod);
Upvotes: 1
Reputation: 9167
public class Character // C should be uppercase here.
{
public string Name, Owner;
public int Str, Con, Dex, Int, Wis, Cha, AC, Speed, maxHP, currHP, AP, SV, Surges;
public double ModMe()
{
return Math.Floor(this.Str / 2); // Math.Floor returns double
}
}
character c = new character();
c.Name = "Goofy";
c.Owner = "Me";
c.Str = 15;
MessageBox.Show(c.ModMe());
Upvotes: 3
Reputation: 1534
public class character
{
public string Name, Owner;
public int Str, Con, Dex, Int, Wis, Cha, AC, Speed, maxHP, currHP, AP, SV, Surges;
public int MathFunction(int input)
{
return (int)(input/2);
}
}
now you can use
MessageBox.Show(c.MathFunction(c.Str));
Upvotes: -2