2012-04-19 133 views
0

我试图让投资的名称出现在股票的列表框中,但代码(我之前在程序中使用过)似乎没有工作。从文本框填充列表框

namespace JamesClemens_FinalProject 
{ 
public partial class Form1 : Form 
{ 
    ArrayList account; 

    public Form1() 
    { 
     InitializeComponent(); 
     account = new ArrayList(); 
    } 

    //here we set up our add customer button from the first tab 
    //when the information is filled in and the button is clicked 
    //the name on the account will be put in the combobox on the second tab 
    private void btnAddCustomer_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      CustomerAccount aCustomerAccount = new CustomerAccount(txtAccountNumber.Text, txtCustomerName.Text, 
      txtCustomerAddress.Text, txtPhoneNumber.Text); 
      account.Add(aCustomerAccount); 

      cboClients.Items.Add(aCustomerAccount.GetCustomerName()); 
      ClearText(); 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("Make sure every text box is filled in!", "Error", MessageBoxButtons.OK); 
     } 
    } 


    private void ClearText() 
    { 
     txtAccountNumber.Clear(); 
     txtCustomerName.Clear(); 
     txtCustomerAddress.Clear(); 
     txtPhoneNumber.Clear(); 
    } 


    private void cboClients_SelectedIndexChanged(object sender, EventArgs e) 
    { 

     CustomerAccount custAccount = account[cboClients.SelectedIndex] as CustomerAccount; 
if(custAccount != null) 
{ 
    txtAccountNumberTab2.Text = custAccount.GetAccountNumber(); 
    txtCustomerNameTab2.Text = custAccount.GetCustomerName(); 
    txtCustomerAddressTab2.Text = custAccount.GetCustomerAddress(); 
    txtCustomerPhoneNumberTab2.Text = custAccount.GetCustomerPhoneNo(); 
} 
    } 

这是给我找麻烦的代码,它口口声声说“新股票”不包含一个构造函数四个参数。

private void btnAddStock_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      Stock aStock = new Stock(txtInvestmentID.Text, txtInvestmentName.Text,  txtInvestmentSymbol.Text, 
       int.Parse(txtInvestmentShares.Text)); 
      account.Add(aStock); 
      lstStock.Items.Add(aStock.GetInvestmentName()); 
      ClearText(); 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("Make sure every text box is filled in!", "Error", MessageBoxButtons.OK); 
     } 
    } 

没有人知道它为什么不让我用股票类?

这就是你要求的股票类。 public class Stock:投资 { //属性 private double stockPrice;

//constructors 
    public Stock() 
    { 
    } 

    public Stock(string anInvestmentID, string anInvestmentName, string anInvestmentSymbol, 
     int anInvestmentShare, CustomerAccount aCustomer, double aStockPrice) 
     : base(anInvestmentID, anInvestmentName, anInvestmentSymbol, anInvestmentShare) 
    { 
     SetStockPrice(aStockPrice); 
    } 

    // 
    public Stock(string anInvestmentID, string anInvestmentName, string anInvestmentSymbol, 
     int anInvestmentShare, CustomerAccount aCustomer, double aStockPrice) 
    { 
     SetInvestmentID(anInvestmentID); 
     SetInvestmentName(anInvestmentName); 
     SetInvestmentSymbol(anInvestmentSymbol); 
     SetInvestmentShare(anInvestmentShare); 
     SetStockPrice(aStockPrice); 
    } 

    //set accessors 
    public void SetStockPrice(double aStockPrice) 
    { 
     stockPrice = aStockPrice; 
    } 

    //get accessors 
    public double GetStockPrice() 
    { 
     return stockPrice; 
    } 
+0

您的库存对象不需要四个参数,并且您使用四个参数。尝试发布您的Stock类的构造函数。 – LarsTech 2012-04-19 23:42:06

+0

您的库存对象需要六个参数或零个参数。看起来你的基础类投资有四个参数。 – LarsTech 2012-04-19 23:45:59

+0

好吧,我知道我做了什么。我在Stock类中设置了“stockPrice”,然后在该按钮下添加了它。谢谢! – 2012-04-19 23:50:36

回答

5

请确实阅读您收到的错误消息;它使编程更容易。

的错误信息是很清楚的:

"new Stock" does not contain a constructor that takes four arguments. 

您不必构造在Stock类,它四个参数

看看您在Stock课中发布的三个构造函数。计算您需要传递给每个构造函数的参数数量。有没有只接受四个参数?我看到一个不需要论据的人,两个人都有六个论点。

试图了解错误消息的实际文本将在未来编写代码将帮助你很多。大多数(并非全部,但大多数)错误消息具有有意义的内容,指出您实际的问题。 :)