您好我真的需要这个继承项目的帮助。当用户在组合框中选择一个值时,我需要我的表单来显示每个标签中的所有数据。我得到了列表中声明的所有数据,但我不知道为什么当我选择值时标签仍显示ZERO。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void accountNumComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (accountNumComboBox.SelectedIndex == 0)
{
SavingsAccount savings = new SavingsAccount();
ownerIdLabel.Text = savings.OwnerId;
balanceLabel.Text = savings.Balance.ToString("c");
interestLabel.Text = (string.Format("{0}%", savings.Interest));
}
}
private void Form1_Load(object sender, EventArgs e)
{
List<SavingsAccount> savings = new List<SavingsAccount>();
SavingsAccount savings1 = new SavingsAccount();
savings1.OwnerId = "0001";
savings1.AccountNumber = "31-1000";
savings1.Balance = 100m;
savings1.Interest = 0.01;
SavingsAccount savings2 = new SavingsAccount();
savings2.OwnerId = "0002";
savings2.AccountNumber = "31-1001";
savings2.Balance = 1000m;
savings2.Interest = 0.0125;
这是我的SavingsAccount类
class SavingsAccount : BankAccount
{
//field
private double _interest;
//constructor
public SavingsAccount(string ownerID, string acctNumber, decimal balance, double interest)
: base(ownerID, acctNumber, balance)
{
_interest = interest;
}
//interest property
public double Interest
{
get { return _interest; }
set { _interest = value; }
}
public SavingsAccount()
{
}
}
我很感激你的帮助。谢谢!
答案 0 :(得分:0)
虽然您在下面的代码中声明了值,但您实际上并没有使用它。
private void Form1_Load(object sender, EventArgs e)
{
List<SavingsAccount> savings = new List<SavingsAccount>();
SavingsAccount savings1 = new SavingsAccount();
savings1.OwnerId = "0001";
savings1.AccountNumber = "31-1000";
当您编写下面的代码时,您将在事件内部创建一个新的本地化对象;您之前没有使用过您尝试过的值:
private void accountNumComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (accountNumComboBox.SelectedIndex == 0)
{
SavingsAccount savings = new SavingsAccount();
ownerIdLabel.Text = savings.OwnerId;
balanceLabel.Text = savings.Balance.ToString("c");
我的建议是移动这段代码:List<SavingsAccount> savings = new List<SavingsAccount>();
就在private double _interest
声明的下方。这将使您可以通过其他方法和事件访问它。