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

これは私に問題を引き起こしているコードです。「new Stock」には4つの引数を取るコンストラクターが含まれていないと言い続けています。

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

Stock クラスを使用できない理由を知っている人はいますか?

これが、あなたが求めたストック クラスです。public class Stock:Investment { //プライベート 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;
    }
4

1 に答える 1

5

表示されるエラー メッセージを実際に読んでください。プログラミングがはるかに簡単になります。

エラーメッセージは非常に明確です:

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

Stockクラスに4 つの引数を取るコンストラクターがありません。

Stockクラスに投稿した 3 つのコンストラクターを見てください。各コンストラクターに渡す必要があるパラメーターの数を数えます。4つの引数のみを受け入れるものはありますか? 1 つは引数をとらず、2 つはそれぞれ 6 つの引数をとります。

エラーメッセージの実際のテキストを理解しようとすることは、将来コードを書く際に大いに役立ちます。ほとんどの (すべてではありませんが、ほとんどの) エラー メッセージには、実際の問題を指摘する意味のある内容が含まれています。:)

于 2012-04-19T23:48:16.040 に答える