Let's say I have an object MyCharacter
of the class Character
, which has the following properties: Health
, Mana
, MoveSpeed
.
From a different method I get a string that contains these stats as follows:
"Health: 100 Mana: 100 MoveSpeed: 100"
I now want to assign these stats to my object. My current attempt is this:
// stats is the string I showed above
var statsArray = stats.Split(' ');
for (var i = 0; i < statsArray.Length; i++)
{
switch(statsArray[i])
{
default:
break;
case "Health:":
MyCharacter.Health = statsArray[i+1];
break;
case "Mana:":
MyCharacter.Mana = statsArray[i+1];
break;
case "MoveSpeed:":
MyCharacter.MoveSpeed = statsArray[i+1];
break;
}
}
The thing now is, that I know the order of the stats. It's always Health, then Mana, then MoveSpeed. So I am looking for a way to simplify it, namely getting rid of this switch
(since the actual Character
here has 18 stats and it's not exactly good looking as it is).
My idea would be going through the array and tell the program to assign the first number it finds to Health, the second to Mana and the third to MoveSpeed.
Is anything like that even possible?