C
C#ā€¢2mo ago
iheartmoney.jpeg

addition problem

c# visual studio community windows forms app
No description
20 Replies
iheartmoney.jpeg
iheartmoney.jpegā€¢2mo ago
trying to take an int from balance but it tells me to make it string instead how do i convert string to int
Pobiega
Pobiegaā€¢2mo ago
How do you store the balance?
iheartmoney.jpeg
iheartmoney.jpegā€¢2mo ago
just a label
Pobiega
Pobiegaā€¢2mo ago
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
iheartmoney.jpeg
iheartmoney.jpegā€¢2mo ago
is there a way to like just take the numbers from the label or is that the only wayh
Pobiega
Pobiegaā€¢2mo ago
There is a way, but I'm trying to teach you something important here :p
iheartmoney.jpeg
iheartmoney.jpegā€¢2mo ago
ok thx ill look into it
Pobiega
Pobiegaā€¢2mo ago
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?
iheartmoney.jpeg
iheartmoney.jpegā€¢2mo ago
uhh no i only learned the basics a few days ago and i wanted to make a simple game out of it
Pobiega
Pobiegaā€¢2mo ago
private int _balance;

public int Balance
{
get => _balance;
set
{
_balance = value;
balanceLabel.Text = $"Balance: {_balance}";
}
}
private int _balance;

public int Balance
{
get => _balance;
set
{
_balance = value;
balanceLabel.Text = $"Balance: {_balance}";
}
}
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 label
iheartmoney.jpeg
iheartmoney.jpegā€¢2mo ago
cool! if you have the time can you break down whats happening its ok if u cant
Pobiega
Pobiegaā€¢2mo ago
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 value
iheartmoney.jpeg
iheartmoney.jpegā€¢2mo ago
thanks alot this really hellped helped šŸ˜ one question what is private used for?
Pobiega
Pobiegaā€¢2mo ago
we also give it the modifier "private" so that nothing outside the form can see/access it
iheartmoney.jpeg
iheartmoney.jpegā€¢2mo ago
why wouldnt we want the others not to see it?
Pobiega
Pobiegaā€¢2mo ago
because then they could set the balance directly, instead of going via the property and then your label would not be updated
iheartmoney.jpeg
iheartmoney.jpegā€¢2mo ago
kk thanks
MODiX
MODiXā€¢2mo ago
Pobiega
REPL Result: Success
var account = new Account();
account.Balance = 100;
account.PrintBalance();
account._balance = 200;
account.PrintBalance();

public class Account
{
public int _balance;

public int Balance
{
get => _balance;
set
{
_balance = value;
Console.WriteLine($"Balance changed to {_balance}");
}
}

public void PrintBalance() => Console.WriteLine("Current balance: " + _balance);
}
var account = new Account();
account.Balance = 100;
account.PrintBalance();
account._balance = 200;
account.PrintBalance();

public class Account
{
public int _balance;

public int Balance
{
get => _balance;
set
{
_balance = value;
Console.WriteLine($"Balance changed to {_balance}");
}
}

public void PrintBalance() => Console.WriteLine("Current balance: " + _balance);
}
Console Output
Balance changed to 100
Current balance: 100
Current balance: 200
Balance changed to 100
Current balance: 100
Current balance: 200
Compile: 502.125ms | Execution: 88.047ms | React with āŒ to remove this embed.
Pobiega
Pobiegaā€¢2mo ago
as you can see, if _balance is public, we can use it and we dont get the "balanace changed" message
tatoie dardmeladze
tatoie dardmeladzeā€¢2mo ago
if you did not get it you can try to watch bor code getters and setter it preatty much nails it