【问题标题】:Accessing object from another method in visual studio 2015 form buttons从 Visual Studio 2015 表单按钮中的另一种方法访问对象
【发布时间】:2017-03-22 19:16:45
【问题描述】:

我正在开发一个界面,供用户输入姓名、号码和初始余额以创建帐户。然后允许用户通过一个名为 Customer 的类使用存款和取款方法来取款或存款。

我已成功完成帐户创建部分。问题是我在帐户成功完成后创建了一个名为 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);
        }
    }

【问题讨论】:

    标签: c# forms visual-studio object methods


    【解决方案1】:

    您只需在创建对象的方法之外添加对对象的引用。

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

    假设您需要多个客户,您可能需要使用一个列表。这样您就可以像以前一样创建对象并将其添加到该列表中。

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

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      • 2014-10-13
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      • 2019-07-31
      相关资源
      最近更新 更多