2016-11-09 25 views
0

我工作的界面上爲用戶輸入的名稱,號碼,和初始平衡訪問對象來創建一個帳戶。然後允許用戶通過名爲客戶的存款和取款方法提款或存款。從另一種方法在視覺工作室2015形式的按鈕

我已經成功地完成了創建帳戶部分。問題在於,當帳戶成功完成時,我創建了一個名爲cust1的新對象,但當用戶按下按鈕以應用存款或取款時,無法訪問該對象。

the interface

完整的代碼截至目前:http://pastebin.com/6HHdMLV1

的對象聲明是在這裏這段代碼的底部,這是當用戶按下帳戶創建按鈕的方法。

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); 
     } 
    } 

而這裏是不能訪問cust1對象的方法。

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

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

回答

0

你只需要添加一個參考的對象,您在創建它的方法之外。

private cust1; 

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

而現在只需更換

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

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

假設使y您需要多個客戶,您可能需要使用一個列表。這樣,您可以像創建對象那樣創建對象並將其添加到該列表中。

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) 
    } 

而且通過它的列表上的位置,你可以訪問客戶:

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

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