Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将一个值从一种形式传递到另一种形式的变量,从而实际更改变量的默认值?_C#_Forms_Variables - Fatal编程技术网

C# 如何将一个值从一种形式传递到另一种形式的变量,从而实际更改变量的默认值?

C# 如何将一个值从一种形式传递到另一种形式的变量,从而实际更改变量的默认值?,c#,forms,variables,C#,Forms,Variables,我试图将一种形式的值(增值税)传递给另一种形式的变量(发票总额)。我希望新值覆盖默认值。当我逐步完成代码时,SalesTaxPct变量的值会发生变化,但当我选择calculate时,默认值(7.75)仍会在计算中使用。任何帮助都将不胜感激 增值税表格代码: public frmSalesTax() { InitializeComponent(); } private void btnOK_Click(object sender, EventArg

我试图将一种形式的值(增值税)传递给另一种形式的变量(发票总额)。我希望新值覆盖默认值。当我逐步完成代码时,SalesTaxPct变量的值会发生变化,但当我选择calculate时,默认值(7.75)仍会在计算中使用。任何帮助都将不胜感激

增值税表格代码:

    public frmSalesTax()
    {
        InitializeComponent();
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
        if (IsValidData())
        {
            this.SaveData();
        }  
    }

    private void SaveData()
    {
        string salesTaxPct = Convert.ToString(txtSalesTaxPct.Text);
        this.Tag = salesTaxPct;
        this.DialogResult = DialogResult.OK;
    }
发票总额表代码:

    public frmInvoiceTotal()
    {
        InitializeComponent();
    }
    //removed the constant
    decimal SalesTaxPct = 7.75m;

    private void btnChangePercent_Click(object sender, EventArgs e)
    {
        Form salesTaxForm = new frmSalesTax();
        DialogResult selectedButton = salesTaxForm.ShowDialog();
        if (selectedButton == DialogResult.OK)
        {
            decimal SalesTaxPct = Convert.ToDecimal(salesTaxForm.Tag);
            lblTax.Text = "Tax(" + SalesTaxPct + "%)";
        }
    }

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        if (IsValidData())
        {
            decimal productTotal = Convert.ToDecimal(txtProductTotal.Text);
            decimal discountPercent = .0m;

            if (productTotal < 100)
                discountPercent = .0m;
            else if (productTotal >= 100 && productTotal < 250)
                discountPercent = .1m;
            else if (productTotal >= 250)
                discountPercent = .25m;

            decimal discountAmount = productTotal * discountPercent;
            decimal subtotal = productTotal - discountAmount;
            decimal tax = subtotal * SalesTaxPct / 100;
            decimal total = subtotal + tax;

            txtDiscountAmount.Text = discountAmount.ToString("c");
            txtSubtotal.Text = subtotal.ToString("c");
            txtTax.Text = tax.ToString("c");
            txtTotal.Text = total.ToString("c");

            txtProductTotal.Focus();
        }
    }
public frmInvoiceTotal()
{
初始化组件();
}
//去掉常数
十进制销售税PCT=7.75米;
私有void bTNCHANGE百分比\u单击(对象发送者,事件参数e)
{
表单salesTaxForm=new frmsaleTax();
DialogResult selectedButton=salesTaxForm.ShowDialog();
如果(selectedButton==DialogResult.OK)
{
十进制SalesTaxPct=Convert.ToDecimal(salesTaxForm.Tag);
lblTax.Text=“Tax(“+SalesTaxPct+”)”;
}
}
私有void btnCalculate\u单击(对象发送者,事件参数e)
{
if(IsValidData())
{
十进制productTotal=Convert.ToDecimal(txtProductTotal.Text);
小数折扣百分比=0.0m;
如果(产品总数<100)
折扣百分比=.0百万;
否则如果(productTotal>=100&&productTotal<250)
折扣率=100万美元;
否则如果(productTotal>=250)
折扣率=0.25百万;
小数折扣额=产品总额*折扣百分比;
小数小计=产品总计-折扣金额;
十进制税=小计*销售税百分比/100;
小数合计=小计+税金;
txtdecentamount.Text=discountAmount.ToString(“c”);
txtSubtotal.Text=subtotal.ToString(“c”);
txtTax.Text=tax.ToString(“c”);
txtTotal.Text=total.ToString(“c”);
txtProductTotal.Focus();
}
}

如果您注意到在
btnChangePercent\u单击
中,您可以通过声明其类型(
decimal SalesTaxPct
)来创建一个新的局部变量,该变量由SalesTax表单的报税表正确设置:

if (selectedButton == DialogResult.OK)
{
    // In the net line you're declaring a new, local
    // variable instead of using the class level variable
    decimal SalesTaxPct = Convert.ToDecimal(salesTaxForm.Tag);
    lblTax.Text = "Tax(" + SalesTaxPct + "%)";
}
但是,尚未设置类级别变量
SalesTaxPct
。如果删除
十进制
声明,它将按预期工作:

if (selectedButton == DialogResult.OK)
{
    SalesTaxPct = Convert.ToDecimal(salesTaxForm.Tag);
    lblTax.Text = "Tax(" + SalesTaxPct + "%)";
}

尝试从此行开头删除十进制:
decimal SalesTaxPct=Convert.ToDecimal(salesTaxForm.Tag):看起来您正在声明一个新变量,而不是修改现有变量