C
C#2y ago
Whiteboy

How to access wanted property of a class instance? [Answered]

So i have a class in which i generate it's instances with properties A B C D and i save them to a file, now i want to access e.g C property and determine stuff based on it
27 Replies
Thinker
Thinker2y ago
You mean you want to access the C property from the contents of the file? Or just access the C property in general?
Whiteboy
Whiteboy2y ago
@thinker227 just in general i mean the one way which is easier/simpler i want to access that property then display other stuff based on it e.g C = 1 i display number C = 2 i display photo etc.
Thinker
Thinker2y ago
yourObject.C?
Whiteboy
Whiteboy2y ago
idk it doesn't allow me to unreachable code
Kouhai
Kouhai2y ago
unreachable code means you have a return statement before that statement
Whiteboy
Whiteboy2y ago
@Kouhai can u take a look at it? maybe it's simple and im just overthinking it and i can't explain it properly
Kouhai
Kouhai2y ago
Sure, post the code here $code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Whiteboy
Whiteboy2y ago
public static Dictionary<string, Equipment> Equipments { get; } = new();


//Weapon properties
private int DmgMin { get; set; }
private int DmgMax { get; set; }
private int HitRate { get; set; }
private int CritChance { get; set; }
private int CritDmg { get; set; }
private int Rarity { get; set; }
private int UpgradeLevel { get; set; }
public static Dictionary<string, Equipment> Equipments { get; } = new();


//Weapon properties
private int DmgMin { get; set; }
private int DmgMax { get; set; }
private int HitRate { get; set; }
private int CritChance { get; set; }
private int CritDmg { get; set; }
private int Rarity { get; set; }
private int UpgradeLevel { get; set; }
so this is the bject
Kouhai
Kouhai2y ago
these properties are private, they can only be accessed from within the instance.
Whiteboy
Whiteboy2y ago
does making them public make sense? or should i access them from the file i saved them?
Whiteboy
Whiteboy2y ago
it's other form i wanted to display upgrade level there
Kouhai
Kouhai2y ago
Yes, it would be better to make them public Opening a file and reading from it each time you want these values is way more expensive than simply making them public and accessing them from outside
Whiteboy
Whiteboy2y ago
and update it every time u select new item do i need to make all public or just the one?
Kouhai
Kouhai2y ago
Make the ones you want to access from outside public, change the rest to fields
Whiteboy
Whiteboy2y ago
Whiteboy
Whiteboy2y ago
still can't access it
Kouhai
Kouhai2y ago
Equipments is a dictionary It doesn't have these properties, it has String keys and Equipment values
Whiteboy
Whiteboy2y ago
i have the key
Int32.Parse(button.Name)
Int32.Parse(button.Name)
but can't access the value
Kouhai
Kouhai2y ago
How?
Whiteboy
Whiteboy2y ago
Equipment.Equipments.TryGetValue(Int32.Parse(button.Name), out UpgradeLevel);
Equipment.Equipments.TryGetValue(Int32.Parse(button.Name), out UpgradeLevel);
Error CS0103 The name 'UpgradeLevel' does not exist in the current context
Kouhai
Kouhai2y ago
The error says exactly what needs to be done. UpgradeLevel wasn't declared anywhere A short hand way to declare it on one line would be
Equipment.Equipments.TryGetValue(Int32.Parse(button.Name), out var UpgradeLevel);
Equipment.Equipments.TryGetValue(Int32.Parse(button.Name), out var UpgradeLevel);
Whiteboy
Whiteboy2y ago
Error CS1503 Argument 1: cannot convert from 'int' to 'string what why not it doesn't convert how wasn't declared doesn't the TryGetValue take it from the dictionary? with the key "Int32.Parse(button.Name)" and the output should be the value
Kouhai
Kouhai2y ago
The error is self explanatory, you're trying to use an int as a key when your dictionary uses string as it's keys
Whiteboy
Whiteboy2y ago
SharedClass.UpgradeLvlValue = Equipment.Equipments[button.Name];
SharedClass.UpgradeLvlValue = Equipment.Equipments[button.Name];
fixed it works just fine the dict has key String nto int so and thsi had to be Equipment
public static Equipment UpgradeLvlValue { get; set; }
public static Equipment UpgradeLvlValue { get; set; }
Accord
Accord2y ago
✅ This post has been marked as answered!