I have a few resources set in my class – coins, diamonds, hearts, enum of these resources names, and method for increasing specified resource. I want to make single "Increase" method for all resources that wouldn't have switch statement. Sort of single-line not overloaded "generic" method independently on which property to increase. Below is the best what I achieved so far. Please advice on best approach to be taken for the described Increase method.
public enum ResourceType { Coins, Diamond, Hearts };
private static int Coins { get { return resourceBox[ResourceType.Coins]; } }
private static int Diamonds { get { return resourceBox[ResourceType.Diamonds]; } }
private static int Hearts { get { return resourceBox[ResourceType.Hearts]; } }
private static Dictionary<ResourceType, int> resourceBox = new Dictionary<ResourceType, int>();
private void Init()
{
resourceBox.Add(ResourceType.Coins, 0);
resourceBox.Add(ResourceType.Diamond, 0);
resourceBox.Add(ResourceType.Hearts, 0);
}
public static void Increase(ResourceType _res, int _income)
{
resourceBox[_res] += _income;
}
However this leads to situation where Dictionary is a primary resource storage, which may not be desired in future. I wonder what could be another (not obligatory dictionary) way to create Increase method as described above.
code
public class Resources {}/code
\$\endgroup\$ – Madcode Mar 22 '17 at 18:47code public static void Spend(ResourceType _res, decimal _price) { switch (_res) { case ResourceType.Coins: Coins -= _price; break; case ResourceType.Diamond: Diamonds -= _price; break; case ResourceType.Hearts: Hearts -= _price; break; default: break; } if (OnResourceChanged != null) OnResourceChanged(_res, _price); }
\$\endgroup\$ – Madcode Mar 22 '17 at 18:58