shrimppunk shrimppunk - 25 days ago 13
C# Question

Accessing object from another method in visual studio 2015 form buttons

I'm working on an interface for a user to enter a name, number, and initial balance to create an account. Then allowing the user to withdraw or deposit money through a class called Customer with Deposit and Withdrawal methods.

I've done the account creation part successfully. The problem is that I've created a new object called cust1 when the account successfully completes, but that object cannot be accessed when the user presses the button to apply the deposit or withdrawal.

the interface

The full code as of right now: http://pastebin.com/6HHdMLV1

The object declaration is at the bottom of this code here, this is the method for when the user presses the account create button.

private void CreateAccountButton_Click(object sender, EventArgs e)
{
string fullName = " ";
int accountNumber = 0;
double initialBalance = 0.00;
Boolean validName = false;
Boolean validAccount = false;
Boolean validBalance = false;

if (string.IsNullOrWhiteSpace(AccountNameInput.Text))
{
MessageBox.Show("Please enter your full name.", "Error",
MessageBoxButtons.OKCancel);
}
else if(Regex.IsMatch(AccountNameInput.Text, @"^[a-zA-Z ]+$"))
{
if(AccountNameInput.Text.Contains(" "))
{
fullName = AccountNameInput.Text;
validName = true;
}
else
{
MessageBox.Show("Full name must contain a space.", "Error",
MessageBoxButtons.OKCancel);
}
}
else
{
MessageBox.Show("Name must not contain numbers or special characters.", "Error",
MessageBoxButtons.OKCancel);
}

if (string.IsNullOrWhiteSpace(AccountNumberInput.Text))
{
MessageBox.Show("Please enter your account number", "Error",
MessageBoxButtons.OKCancel);
}
else if(Regex.IsMatch(AccountNumberInput.Text, @"^[0-9]+$"))
{
accountNumber = Convert.ToInt32(AccountNumberInput.Text);
validAccount = true;
}
else
{
MessageBox.Show("Must contain only numbers.", "Error",
MessageBoxButtons.OKCancel);
}

if (string.IsNullOrWhiteSpace(InitialBalanceInput.Text))
{
MessageBox.Show("Please enter your initial balance", "Error",
MessageBoxButtons.OKCancel);
}
else if (Regex.IsMatch(AccountNumberInput.Text, @"^[0-9.]+$"))
{
initialBalance = Math.Round(Convert.ToDouble(InitialBalanceInput.Text),2);
validBalance = true;
}
else
{
MessageBox.Show("Initial balance must contain only numbers and a decimal point.", "Error",
MessageBoxButtons.OKCancel);
}

if(validName == true && validAccount == true && validBalance == true)
{
AccountBalanceDisplay.Text = "$" + Convert.ToString(initialBalance);
Customer cust1 = new Customer(fullName, accountNumber, initialBalance);
}
}


And here is the method that isn't able to access the cust1 object.

private void ApplyButton_Click(object sender, EventArgs e)
{
double userInput = Convert.ToDouble(AmountInput.Text);

if (DepositRButton.Checked)
{
cust1.Deposit(userInput);
}
}

Answer

You simply need to add a reference to the object outside of the method that you create it in.

    private cust1;

    private void CreateAccountButton_Click(object sender, EventArgs e)
    {
        //Your code
    }

And now you can just replace

Customer cust1 = new Customer(fullName, accountNumber, initialBalance);

with

cust1 = new Customer(fullName, accountNumber, initialBalance);

Assuming that you need more than one customer you might want to use a list. This way you can just create the object like you did and add it to that list.

List<Customer> CustomerList = new List<Customer>();

    private void CreateAccountButton_Click(object sender, EventArgs e)
    {
        //Rest of your code
        if(validName == true && validAccount == true && validBalance == true)
        {
            AccountBalanceDisplay.Text = "$" + Convert.ToString(initialBalance);
            Customer cust1 = new Customer(fullName, accountNumber, initialBalance);
            CustomerList.Add(cust1)
    }

And you can access the customer by it's position on the list:

private void ApplyButton_Click(object sender, EventArgs e)
{
    double userInput = Convert.ToDouble(AmountInput.Text);

    if (DepositRButton.Checked)
    {
        CustomerList[0].Deposit(userInput);
    }
}
Comments