Ben_T
Ben_T
CC#
Created by Ben_T on 4/7/2023 in #help
❔ Accessing variable from another class
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
21 replies
CC#
Created by Ben_T on 4/7/2023 in #help
❔ Accessing variable from another class
(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?
21 replies
CC#
Created by Ben_T on 4/7/2023 in #help
❔ Accessing variable from another class
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");
}
21 replies
CC#
Created by Ben_T on 4/7/2023 in #help
❔ Accessing variable from another class
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?
21 replies