I have this one generic class called Stat:
public class Stat<T>
{
public string Name;
public T Value;
public Stat(string statName = "Stat")
{
Name = statName;
}
public Stat(T val)
{
Value = val;
}
}
And I want to overload the + operator so that I can combine two stats, and get a stat object with the combined values (and same name as the stat object being returned to).
My idea was this:
public static Stat<T> operator+ (Stat<T> stat1, Stat<T> stat2)
{
Stat<T> result = new Stat<T>();
//result.Value = stat1.Value + stat2.Value; //<- doesnt work
return result;
}
But, as you can see, I can't combine the values of the two stat objects. When I tried adding a where clause to this function, like so:
public static Stat<T> operator+ (Stat<T> stat1, Stat<T> stat2)
where T: struct, IComparable, IComparable<T>, IEquatable<T>
{
Stat<T> result = new Stat<T>();
//result.Value = stat1.Value + stat2.Value; //<- doesnt work
return result;
}
That's apparently a syntax error; says "the type or namespace 'where' cannot be found".
So, how do I have the values of the two stats be added? Also, how do I keep the name of the object being returned to intact? That last part is where I'm most lost on.
Edit: I learned how to get the values of the stats added a while ago, but keeping the name of one intact is the problem.
Also, this question is not a duplicate of this one. That one asks how to add two objects, resulting in their values being added in the new object. This one goes a step further by also asking how to keep one of the object's fields the same.