C
C#2y ago
Ben_T

❔ 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!
11 Replies
Dultus
Dultus2y ago
Yes, that's correct. As you pointed out you're trying to access an empty instance. If you're planning to do a list of customers, I'd introduce an Id to them and a list of customers. You can create the list for the entire class or as a static object so you can access it from anywhere. Next, pick your customer from the list and then check the balance. An option could be to make a temporary variable like selectedCustomer that you set the selected customer to so you don't have to constantly access the list and search for it.
Ben_T
Ben_TOP2y ago
well I only need one balance at a timeand its saved in a sql database and pulled from the database when the user logs in (which includes an ID but that ID number is not accessible to give the customer anonymity in the future) but all the information about the customer is saved in the Customer class. But if I were to make a list in the Customer class and add the balance and ID to it, wouldnt I run into the same problem of accessing a new instance of the class, just this time I'm accessing an empty list?
Dultus
Dultus2y ago
Yeah, that was missing information. A trmporary customer class would be the best option then imo. So a select method that writes the customer into a class variable or a static variable which you then access.
Ben_T
Ben_TOP2y ago
so would that be something like this?
private CustomerForm _customer;

public MoneyOrder(CustomerForm customer)
{
_customer = customer;
}

public int CustomerBalance
{
get { return _customer.Balance; }
}

public void NewMethod()
{
CustomerForm cust = new();
MoneyOrder moneyOrder = new MoneyOrder(cust);
int balance = moneyOrder.CustomerBalance;

MessageBox.Show(balance.ToString(), "Customer Balance");
}
private CustomerForm _customer;

public MoneyOrder(CustomerForm customer)
{
_customer = customer;
}

public int CustomerBalance
{
get { return _customer.Balance; }
}

public void NewMethod()
{
CustomerForm cust = new();
MoneyOrder moneyOrder = new MoneyOrder(cust);
int balance = moneyOrder.CustomerBalance;

MessageBox.Show(balance.ToString(), "Customer Balance");
}
Dultus
Dultus2y ago
You still create new instances of the class you're trying to access. When you do so of course you're just getting 0. It really depends on where you want to call it. This way with your new method you just created a new customer.
Ben_T
Ben_TOP2y ago
(This is a windows forms app, sorry I did not mention sooner) Okay so within my Customer class I created this
public static CustomerForm SelectedCustomer { get; set; }

public void Select()
{
SelectedCustomer = this;
}
public static CustomerForm SelectedCustomer { get; set; }

public void Select()
{
SelectedCustomer = this;
}
And then in my login class I called the CustomerForm.Select() and then in my MoneyOrder class I have this
CustomerForm cust = new();
int balance = CustomerForm.SelectedCustomer.Balance;
CustomerForm cust = new();
int balance = CustomerForm.SelectedCustomer.Balance;
this uses a static variable to save the information set in the instance and i save it when logging in and then can use the information that way. Is there anything you see wrong with my code here that could cause problems? I was thinking if I log out (not created 'yet') then when I log back in will there be an issue as the static variable is already set?
phaseshift
phaseshift2y ago
Yes, this is not good since anything can change that static variable at any time
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Ben_T
Ben_TOP2y ago
So what would be the proper solution? I don’t need to modify the balance, I just need the value so changing the values isn’t really an issue but I want to know what the proper way to do it would be in my scenario
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server