I have a class called Weapon which stores public fields called Strength, Dexterity, Vitality, Intelligence. (type Int32)
In the constructor of the weapon object i have a method which randomly chooses one of these 'Attributes' and assigns a value to it.
Random random = new Random();
int primary = random.Next(0, 4);
if (//Condition met)
{
switch (primary)
{
case 0:
Strength = ...
break;
case 1:
Dexterity = ...
break;
case 2:
Vitality = ...
break;
case 3:
Intelligence = ...
break;
}
}
I find this code ugly and hard to maintain.
Is there a way to store these 'Attributes' in a Array or List? Random would then choose between 0 and Attributes.Length and assign a value accordingly.
Kind of like this (this wont work just so you understand)
Random random = new Random();
int[] attributes = {this.Strength, this.Dexterity, this.Vitality, this.Intelligence};
int index = random.Next(0, attributes.Length);
//lets say 0, so index = Strength
if (//Condition met)
{
//This is the part where index would be 0 so the program would assign 'Strength' a value
}
The value should be assigned to the field chosen in the Array. Not the element in the Array.
This would greatly help when later developing inheriting classes