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
You mean you want to access the C property from the contents of the file?
Or just access the C property in general?
@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.
yourObject.C
?idk it doesn't allow me to
unreachable code
unreachable code means you have a return statement before that statement
@Kouhai can u take a look at it?
maybe it's simple and im just overthinking it
and i can't explain it properly
Sure, post the code here
$code
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/
so this is the bject
these properties are private, they can only be accessed from within the instance.
does making them public make sense? or should i access them from the file i saved them?
it's other form i wanted to display upgrade level there
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
and update it every time u select new item
do i need to make all public
or just the one?
Make the ones you want to access from outside public, change the rest to fields
still can't access it
Equipments
is a dictionary
It doesn't have these properties, it has String
keys and Equipment
valuesi have the key
but can't access the value
How?
Error CS0103 The name 'UpgradeLevel' does not exist in the current context
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
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
The error is self explanatory, you're trying to use an
int
as a key when your dictionary uses string
as it's keys
fixed it
works just fine
the dict has key String nto int so
and thsi had to be Equipment
✅ This post has been marked as answered!