discountPercent是一个变量但是像方法一样使用

时间:2015-05-26 00:54:59

标签: c#

我是编程新手。我试图修改代码以使用Math.round方法代替Convert.ToString,这里的事情令人困惑。我已经研究了这个错误,但仍然没有看到该怎么做。谢谢你的帮助。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InvoiceTotal
{
    public partial class frmInvoiceTotal : Form
    {
        public frmInvoiceTotal()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnCalculate_Click(object sender, System.EventArgs e)
        {
            decimal Subtotal = Decimal.Parse(txtSubtotal.Text);
            decimal discountPercent = 0m;

            if (Subtotal >= 500)
            {
                discountPercent = .2m;
            }

            else if (Subtotal >= 250 && Subtotal < 500)
            {
                discountPercent = .15m;
            }

            else if (Subtotal >= 100 && Subtotal < 250)
            {
                discountPercent = .1m;
            }

            decimal discountAmount = Math.Round(discountAmount, 2);
            decimal invoiceTotal = Math.Round(invoiceTotal, 2);

            txtDiscountPercent.Text = discountPercent("p1");
            txtDiscountAmount.Text = discountAmount("c");
            txtTotal.Text = invoiceTotal.ToString("c");

            txtSubtotal.Focus();
        }

        private void btnExit_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmInvoiceTotal_Load(object sender, EventArgs e)
        {

        }


    }
}

-----------------------
Error List:
Error   1   'discountPercent' is a 'variable' but is used like a 'method'
Error   1   'discountAmount' is a 'variable' but is used like a 'method'    

1 个答案:

答案 0 :(得分:2)

正如错误所说,discountPercent是变量,而不是方法。您需要在.ToString()变量上使用discountPercent方法。

txtDiscountPercent.Text = discountPercent.ToString("p1");
txtDiscountAmount.Text = discountAmount.ToString("c");