gpt4 book ai didi

c# - InterestRate 正在返回 0,我使用的引用来自另一个 Form

转载 作者:行者123 更新时间:2023-11-30 14:26:01 25 4
gpt4 key购买 nike

我是 C# 的新手,今天我已经在这里发过一次帖,非常感谢能得到的所有帮助!我有一个用 Visual Basic 创建的程序。该表单有一个主表单 - AccountForm.cs,三个副表单 - WithdrawlForm.csDepositForm.cs InterestForm.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);
}

完整的 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);
}
}
}

这是我的 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();
}


}
}

最后,这是我的 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;
}


}
}

最佳答案

编辑:

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

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

请注意,您使用本地 interestRate不是你的类(class)'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();
}

原文:

settergetterInterestRate似乎...不对称:

double InterestRate 
{
get { return interestForm.interestRate ; } //you get from other form
set { interestRate = interestForm.interestRate; } //and you set from other form
}

你得到 interestForm.interestRate来自其他表格并将其设置为 interestRate在你的Form但你从不使用本地 interestRate .将其更改为:

double InterestRate 
{
get { return interestRate ; }
set { interestRate = value; }
}

您的 Interest 也是如此:

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

您再次使用您的私有(private) Form除了被构造之外,它在类中未被使用。使用你的 amount相反:

public double Interest(double amount)
{
InterestRate = amount; //change into this
return InterestRate;
}

现在,还要注意:

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

你有 InterestForm在那里声明,但它是一个 new InterestForm , 这不是你的 private InterestForm在你的SavingAccount .简而言之,InterestForm你声明和InterestForm在你的SavingAccount两个不同的表单实例,尽管它们共享相同的名称 interestForm .查看访问修饰符 - 不是实例的名称 - 以了解您的 object 的范围.

也就是说,上面的设计使用非字段InterestForm填满你的兴趣就可以了。我什至会说,使用 private InterestForm不需要。您只需要将值传递给外部 InterestForm有你的SavingAccount实例,不创建私有(private) InterestForm为你的 SavingAccount

关于c# - InterestRate 正在返回 0,我使用的引用来自另一个 Form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36612680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com