InterestRate返回0,我使用的引用来自另一个Form

R. *_*iff 3 c#

C#的新手,今天我已经在这里发布了一次,我感谢所有的帮助!我有一个我在Visual Basic中创建的程序.窗体有一个主表-的AccountForm.cs,三道关拍表格- WithdrawlForm.cs,DepositForm.csInterestForm.cs.我有两个类,Account.cs及其派生类SavingsAccount.cs.我试图将InterestRate下面的字符串更改为用户输入的利率InterestForm.此代码位于SavingsAccount类.cs文件中.

     public new string toString()
    {
        return String.Format("Account Number: {0} Balance: {1:C} Interest Rate: {2:P}", AccountNumber, AccountBalance, InterestRate);
    }  
Run Code Online (Sandbox Code Playgroud)

完整的SavingsAccount.cs文件

namespace Assignment_6_third_attempt
{
public class SavingsAccount : Account
{
    private InterestForm interestForm = new InterestForm();

    private double interestRate = 0.0;

    #region Constructors
    public SavingsAccount(int accountNumber)
    {
        base.accountNumber = accountNumber;
    }

    public SavingsAccount() { }
    #endregion

   double InterestRate 
   {
        get { return interestForm.interestRate ; }
        set { interestRate = interestForm.interestRate; }
    }

    public double Interest(double amount)
    {
        InterestRate = interestForm.interestRate;
        return InterestRate;          
    }


    public void AddInterest(double amount)
    {
        //interestForm.CalculateInterest();
        AccountBalance = amount;
    }


    public new string toString()
    {
        return String.Format("Account Number: {0} Balance: {1:C} Interest Rate: {2:P}", AccountNumber, AccountBalance, InterestRate);
    }       
}
}
Run Code Online (Sandbox Code Playgroud)

这是我的AccountForm.cs文件(此表格是主表格,其中显示了帐号,帐户余额和利率.

 namespace Assignment_6_third_attempt
{
public partial class AccountForm : Form
{
    SavingsAccount savingsAccount;
    public System.Windows.Forms.Label labelInformation;

    public AccountForm()
    {
        InitializeComponent();
        labelInformation = accountInfoLabel;
    }

    private void createAccountButton_Click(object sender, EventArgs e)
    {
        Random random = new Random();
        int newAccountNumber = random.Next(0, 1000000);
        savingsAccount = new SavingsAccount(newAccountNumber);

        MessageBox.Show(savingsAccount.toString());
        accountInfoLabel.Text = savingsAccount.toString();
        depositButton.Enabled = true;
        withdrawalButton.Enabled = true;
        interestButton.Enabled = true;
    }

    private void depositButton_Click(object sender, EventArgs e)
    {
        DepositForm depForm = new DepositForm();
        depForm.ShowDialog();
        savingsAccount.Deposit((double)depForm.DepositAmount);
        accountInfoLabel.Text = savingsAccount.toString();
    }

    private void withdrawalButton_Click(object sender, EventArgs e)
    {
        try
        {
            WithdrawalForm withdrawForm = new WithdrawalForm();
            withdrawForm.ShowDialog();
            savingsAccount.Withdraw((double)withdrawForm.WithdrawAmount);
            accountInfoLabel.Text = savingsAccount.toString();
        }
        catch (InvalidOperationException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void closeAccountButton_Click(object sender, EventArgs e)
    {
        depositButton.Enabled = false;
        withdrawalButton.Enabled = false;
        accountInfoLabel.Text = "";

        savingsAccount = null; //this will clean it up off the heap.
    }



    private void interestButton_Click(object sender, EventArgs e)
    {
        InterestForm interestForm = new InterestForm(savingsAccount.AccountBalance);
        interestForm.ShowDialog();
        savingsAccount.AddInterest(interestForm.CalculateInterest());           
        accountInfoLabel.Text = savingsAccount.toString();
        savingsAccount.Interest(interestForm.interestRate);
        accountInfoLabel.Text = savingsAccount.toString();
        }


    }
}
Run Code Online (Sandbox Code Playgroud)

最后,这是我的InterestForm.cs代码,用户可以在NumericUpDown控件中输入术语和速率.

 namespace Assignment_6_third_attempt
{
 public partial class InterestForm : Form
{

    private Account account = new Account();
    //private double accountBalance;
    public double interestRate = 0.0;

    public InterestForm()
    {
        InitializeComponent();
    }


    public InterestForm(double accountBalance) : this()
    {

        //InitializeComponent();
        account.AccountBalance = accountBalance;
    }


    private void addInterestButton_Click(object sender, EventArgs e)
    {
        double interestRate = double.Parse(interestUpDown.Text);
        CalculateInterest();
        this.Close();
    }



    public double CalculateInterest()
    {
        //A = P(1 + rt)
        double accountBalance = account.AccountBalance;            
        double interestRate = (double)interestUpDown.Value;
        double term = (double)termUpDown.Value;
        double estimateInterest = (accountBalance * (1 + (interestRate * term)));
        return estimateInterest;
    }


}
}
Run Code Online (Sandbox Code Playgroud)

Ian*_*Ian 5

编辑:

除了提到的所有原始问题之外,您似乎在这里将您的interestRate private 字段范围与interestRate 局部变量混淆:

private void addInterestButton_Click(object sender, EventArgs e)
{
    double interestRate = double.Parse(interestUpDown.Text); //this is local variable!!
    CalculateInterest();
    this.Close();
}
Run Code Online (Sandbox Code Playgroud)

请注意,您使用的是本地 interestRate,而不是您的班级interestRate.同样,要小心变量的范围.无法全局访问局部变量,如果在本地方法中具有相同的变量名,则必须将其与使用this关键字的全局变量(类'字段)区分开来(this.VariableName字段VariableName是本地的).

在上面的例子中,只需使用class字段interestRate而不是定义新的局部变量interestRate:

private void addInterestButton_Click(object sender, EventArgs e)
{
    interestRate = double.Parse(interestUpDown.Text); //remove the double
    CalculateInterest();
    this.Close();
}
Run Code Online (Sandbox Code Playgroud)

原版的:

settergetterInterestRate似乎... 不对称:

double InterestRate 
{
    get { return interestForm.interestRate ; } //you get from other form
    set { interestRate = interestForm.interestRate; } //and you set from other form
}
Run Code Online (Sandbox Code Playgroud)

interestForm.interestRate从其他形式获得并将其设置为interestRate你的Form但你从不使用本地interestRate.将其改为:

double InterestRate 
{
    get { return interestRate ; }
    set { interestRate = value; }
}
Run Code Online (Sandbox Code Playgroud)

你的情况也是如此Interest:

public double Interest(double amount)
{
    InterestRate = interestForm.interestRate;
    return InterestRate;          
}
Run Code Online (Sandbox Code Playgroud)

你再次使用你的私人Form,除了被构建之外,在课堂上没用.使用你的amount代替:

public double Interest(double amount)
{
    InterestRate = amount; //change into this
    return InterestRate;          
}
Run Code Online (Sandbox Code Playgroud)

现在,请注意:

private void interestButton_Click(object sender, EventArgs e)
{
    InterestForm interestForm = new InterestForm(savingsAccount.AccountBalance);
    interestForm.ShowDialog();
    savingsAccount.AddInterest(interestForm.CalculateInterest());           
    accountInfoLabel.Text = savingsAccount.toString();
    savingsAccount.Interest(interestForm.interestRate);
    accountInfoLabel.Text = savingsAccount.toString();
}
Run Code Online (Sandbox Code Playgroud)

你已经InterestForm宣布了,但它是一个new InterestForm,它不是你private InterestFormSavingAccount.简而言之,虽然它们共享相同的名称,但InterestForm您声明并且InterestForm在您SavingAccount两个不同的表单实例中interestForm.查看访问修饰符 - 而不是实例的名称 - 以了解您的范围object.

话虽如此,上面的设计使用非字段InterestForm来填补你的兴趣是可以的.我甚至会说,InterestForm不需要使用私人.你只需要外部的值传递InterestForm有你的SavingAccount实例,而不是创建私有InterestFormSavingAccount