My method searches through an list of accounts which have the property "Name".
class Accounts
{
static internal List<Account> accounts = new List<Account>();
static internal bool findAccount(string name, ref int account)
{
for (int i = 0; i < accounts.Count; i++)
{
if (accounts[i].Name == name)
{
account = i;
return true;
}
}
return false;
}
}
The variable it is referencing is in the main form:
public partial class Lodge : Form
{
public Lodge()
{
InitializeComponent();
}
private void btn_lodge_Click(object sender, EventArgs e)
{
int account = -1;
bool found = Accounts.findAccount(tBox_wName.Text, ref account);
if (found && account >= 0)
{
decimal lodge = Convert.ToDecimal(tBox_tAmou.Text);
Accounts.accounts[account].lodge(lodge);
lbl_stats.Text = "Successfully lodged. Balance is now: " + Accounts.accounts[account].Balance;
}
else
{
lbl_stats.Text = "Account not found.";
}
}
I am still learning C#, so I am still learning the most efficient ways of doing things. This code works as it should, but I feel like the fact that I am returning a boolean and referencing the account variable seems wrong.
The program is basically lodging money to an account (simply an object containing the person's name, etc..). So there is a static Accounts class and a non-static class 'Account' for each account. I have made a static class because these functions are to be shared across multiple forms.
Should I remove the boolean and simply have it return the account to the account variable? What would be the most accepted way by a more experience programmer?