20 Replies
trying to take an int from balance but it tells me to make it string instead
how do i convert string to int
How do you store the balance?
just a label
Right. You should separate your data from where/how you display it
labels display strings. You dont want to be converting back and forth when manipulating your data.
Much better to store the balance in a numeric variable, and just display it in the label
is there a way to like just take the numbers from the label or is that the only wayh
There is a way, but I'm trying to teach you something important here :p
ok thx
ill look into it
The best way would be with a property, a backing field and a custom setter that also updates the label.
Do you know what properties, fields and setters are?
uhh no i only learned the basics a few days ago and i wanted to make a simple game out of it
whenever you want to change the players balance, you just do it via the property, like so:
Balance = 500;
or Balance += 100;
it will update the variable and also update the labelcool!
if you have the time can you break down whats happening
its ok if u cant
private int _balance;
is the declaration of a field. we also give it the modifier "private" so that nothing outside the form can see/access it
the rest is the property declaration. We give it a "public" modifier, which means other pieces of code can see and interact with it
inside that property declaration, there are two important parts, the getter and the setter
the getter, we just say "hey, when getting the value, just take the value from the _balance field"
the setter does the same, but for writing - it updates the value in the private field, setting it to the value given. note how value
shows up as a keyword here.
then the last line just updates the label, as I'm sure you already know how to do
but we do that as part of the setter in the property, so that we dont have to remember to do it everytime/where we change the valuethanks alot
this really hellped
helped
š
one question what is private used for?
we also give it the modifier "private" so that nothing outside the form can see/access it
why wouldnt we want the others not to see it?
because then they could set the balance directly, instead of going via the property
and then your label would not be updated
kk thanks
Pobiega
REPL Result: Success
Console Output
Compile: 502.125ms | Execution: 88.047ms | React with ā to remove this embed.
as you can see, if
_balance
is public, we can use it and we dont get the "balanace changed" messageif you did not get it you can try to watch bor code getters and setter it preatty much nails it