【发布时间】:2017-03-22 19:16:45
【问题描述】:
我正在开发一个界面,供用户输入姓名、号码和初始余额以创建帐户。然后允许用户通过一个名为 Customer 的类使用存款和取款方法来取款或存款。
我已成功完成帐户创建部分。问题是我在帐户成功完成后创建了一个名为 cust1 的新对象,但是当用户按下按钮申请存款或取款时,无法访问该对象。
截至目前的完整代码: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);
}
}
【问题讨论】:
标签: c# forms visual-studio object methods