- Home /
Question by
Leonid · Oct 28, 2020 at 09:44 AM ·
c#serializationvariablesvaluedictionary
Reference to value type
Hello! I have a class for storing ID's of equipment (armor, weapon, etc.). So there is a lot of integer variables serialized in this class.
int armorId;
int helmetId;
int weaponId;
Therefore I need to use "switch" statement to get/set the value I need.
switch (itemType)
{
case ItemType.Armor: return armorId;
case ItemType.Helmet: return helmetId;
case ItemType.Weapon: return weaponId;
}
It works, but its kind of dull and slow, especially if you have a lot of ID's. So the question is - how can I access those variables by reference? Is there any way to, for examle, put all the references into Dictionary and then to modify them through that dictionary?
private Dictionary<ItemType, int> itemIdsDictionary= new Dictionary<ItemType, int>();
itemIdsDictionary.Add(ItemType.Armor, ref armorId);
itemIdsDictionary.Add(ItemType.Helmet, ref helmetId);
itemIdsDictionary.Add(ItemType.Weapon, ref weaponId);
itemIdsDictionary.[ItemType.Armor] = 2; //set armorId to 2
Comment
Answer by frknerstr · Oct 28, 2020 at 12:17 PM
You can use enum and get enum int value like this
int value = (int)SomeEnum.SomeItem;
Well thanks, but ... Have you read and understood the question?