Ben_T
Ben_T
CC#
Created by Ben_T on 4/7/2023 in #help
❔ Accessing variable from another class
I have a Customer class that has a public Balance variable and it gets properly set within the class as I use it to update labels
public int Balance { get; set; }

private void UpdateLabels()
{
usernameLbl.Text = Username;
balanceAmountLbl.Text = "$" + Balance;
}
public int Balance { get; set; }

private void UpdateLabels()
{
usernameLbl.Text = Username;
balanceAmountLbl.Text = "$" + Balance;
}
When using a different class called MoneyOrder, the user inputs an amount and I try to access that balance to see if the amount is less than the balance. The issue is the balance is returning as 0.
CustomerForm cust = new();
MessageBox.Show(cust.Balance.ToString(), "Customer Balance");
CustomerForm cust = new();
MessageBox.Show(cust.Balance.ToString(), "Customer Balance");
I believe this is because I'm creating a new instance of the Customer class and so the Balance variable is being set to 0. I'm not too sure how to fix this as I've tried some other solutions but haven't gotten anywhere as the balance is always 0 or it causes other parts of the MoneyOrder class to not run so it'd more work just to even test Would love some help on this!
21 replies