-4

次のコードを含むフォームがあります。

public partial class frmSalesTax : Form
{
    public frmSalesTax()
    {
        InitializeComponent();
    }

    //declare variables
    decimal ItemPrice = 00.00m;
    decimal TaxAmount = 00.08m;
    decimal TotalAmount = 00.00m;

    private void btnCalc_Click(object sender, EventArgs e)
    {
        try
        {
            if (decimal.TryParse(txtItemPrice.Text, out ItemPrice))
            {
                //Instantiated instance of a class here. 
                CTransaction Calc;
                Calc = new CTransaction();

                //set properties to calc tax amount.
                Calc.SalesTaxRate = .08m;
                Calc.TxtItemPrice = ItemPrice;

                //call the method in the instance of the class
                TaxAmount = Calc.CalculateTax();

                //Set tax amount property to be available for the calc.
                Calc.CalculateTax = TaxAmount;

                //call the method in the instance of the class.
                TotalAmount = Calc.CalculateTotal();

                //Display the values
                lblTaxAmt.Text = TaxAmount.ToString("c");
                lblTotal.Text = TotalAmount.ToString("c");
            }
            else
            {
                MessageBox.Show("Enter a numeric value please");
                txtItemPrice.Focus();
                txtItemPrice.SelectAll();

                lblTaxAmt.Text = string.Empty;
                lblEndTotal.Text = string.Empty;

            }
        }
        catch
        {
            MessageBox.Show("Critical Error");
        }
    }
    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

およびクラス:

public class CTransaction
{
    //Create private fields
    private decimal salesTaxRate = .07m;
    private decimal ItemPrice;
    private decimal taxAmount;

    //Define the properties
    public decimal SalesTaxRate
    {
        get { return salesTaxRate;}
        set { salesTaxRate = value;}
}
    public decimal TxtItemPrice
    {
        get { return ItemPrice; }
        set { ItemPrice = value;}
    }

    //Custom methods
    public decimal CalculateTax()
    {
        return ItemPrice * SalesTaxRate;
    }

    public decimal CalculateTotal()
    {
        return ItemPrice + taxAmount;
    }
}

メソッドグループであるため、「「CalculateTax」に割り当てることはできません。(Form1.cs .. 54行目.. 21列目)

フォームには、ユーザーが txtItemPrice (テキスト ボックス) を操作するための次のフィールドがあります。 3 - ボタン (計算、クリア、終了) + 税額

4

2 に答える 2

4

これは問題の行です:

                //Set tax amount property to be available for the calc.
                Calc.CalculateTax = TaxAmount;

値 (TaxAmount) をメソッド (CalculateTax) に割り当てようとしています。そんなことはできません。税率を設定しようとしている場合は、設定できるようにパブリック プロパティを追加する必要があります。

Calc.TaxAmount = TaxAmount;

次に、Calc クラスで次のようにします。

public decimal TaxAmount
{
    get { return taxAmount; }
    set { taxAmount = value; }
}

その後、すべてが期待どおりに機能するはずです。

于 2016-05-12T21:24:35.157 に答える
0

行 Calc.CalculateTax は、メソッドを介して値を渡すためのメソッドです。パラメーターとして渡す必要があります。

あなたのコードでは、CTransaction クラスに変更を加えます。

public decimal CalculateTotal(decimal taxAmount)
{
     return itemPrice + taxAmount;
}

そして、frmSalesTaxあなたの行を削除するだけです:

//Set tax amount property to be available for the calc.
                Calc.CalculateTax = TaxAmount;

そして、あなたの行でTotalAmount = Calc.CalculateTotal();、メソッドtaxAmountのパラメータとしての変数。TotalAmountそして、それは次のようになるはずです:

TotalAmount = Calc.CalculateTotal(taxAmount);

期待どおりに動作するはずです。

詳細については、次のリンクを確認してください。

C# メソッド

C# パラメータの受け渡し

于 2016-05-12T21:57:43.400 に答える