【问题标题】:How to validate price range value in c#如何在 C# 中验证价格范围值
【发布时间】:2015-07-09 11:14:55
【问题描述】:

我正在为我的销售和库存添加材料。到目前为止,我需要验证添加材料时,销售价格必须大于购买价格。

这是我的代码,我在 if/else 语句中遇到错误,其中我的 txtPurchasePrice 和 txtSellingPrice 的值为十进制 (18, 2)。


protected void btnAdd_Click(object sender, EventArgs e)
{
    if (txtSellingPrice.Text >= txtPurchasePrice.Text)
    {
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandText = "INSERT INTO Materials VALUES (@UnitID, @Name, @SellingPrice, @PurchasePrice, " +
            "@Description, @Available, @CriticalLevel, @Maximum, @Status, @DateAdded, @DateModified)";                
        cmd.Parameters.AddWithValue("@UnitID", txtUnitID.Text);
        cmd.Parameters.AddWithValue("@Name", txtName.Text);
        cmd.Parameters.AddWithValue("@SellingPrice", txtSellingPrice.Text);
        cmd.Parameters.AddWithValue("@PurchasePrice", txtPurchasePrice.Text);
        cmd.Parameters.AddWithValue("@Description", txtDesc.Text);
        cmd.Parameters.AddWithValue("@Available", "0");
        cmd.Parameters.AddWithValue("@CriticalLevel", txtCritical.Text);
        cmd.Parameters.AddWithValue("@Maximum", txtMax.Text);
        cmd.Parameters.AddWithValue("@Status", "Available");
        cmd.Parameters.AddWithValue("@DateAdded", DateTime.Now);
        cmd.Parameters.AddWithValue("@DateModified", DBNull.Value);
        cmd.ExecuteNonQuery();
        con.Close();
        Helper.AddLog("1", "Add", "Added a new Material");
        Response.Redirect("~/Materials/Default.aspx");
    }
    else
    {
        error.Visible = true;
    }
}

我的错误

CS0019:运算符“>=”不能应用于“字符串”类型的操作数 和'字符串'

为什么不能使用>=<=||<>?在这种情况下?

【问题讨论】:

  • 因为这些运算符用于数字或枚举值(|| 除外)。您认为一个字符串如何“大于或等于”另一个字符串?这个“更大”的主题在 IMO 的文本比较中会很复杂..
  • 只是想和运营商说清楚 :) 谢谢!

标签: c# asp.net


【解决方案1】:

我假设txtSellingPricetxtPurchasePriceTextBox 控件,因此它们的Text 属性是string 类型。您不能使用>=,因为它没有语义意义。您需要先将文本解析为decimal

var sellingPrice = decimal.Parse(txtSellingPrice.Text);
var purchasePrice = decimal.Parse(txtPurchasePrice.Text);

if (sellingPrice >= purchasePrice)
{
   // stuff
}

如果您不确定文本是有效的decimal 值,请使用decimal.TryParse

decimal sellingPrice;
if (!decimal.TryParse(txtSellingPrice.Text, out sellingPrice))
{
   // Not a valid decimal, do something.
}

decimal purchasePrice;
if (!decimal.TryParse(txtPurchasePrice.Text, out purchasePrice))
{
   // Not a valid decimal, do something.
}

if (sellingPrice >= purchasePrice)
{
   // stuff
}

【讨论】:

  • @Jules 什么整数? :)
  • 非常感谢 :) 你确实回答了我的问题,而且它也有效!
【解决方案2】:

一个字符串可以大于另一个字符串吗?

在进行比较之前,您需要将字符串转换为数值。

既然你在处理金钱,你应该使用Decimal

if (Decimal.Parse(txtSellingPrice.Text) >= Decimal.Parse(txtPurchasePrice.Text))

【讨论】:

    【解决方案3】:

    要比较两个字符串的数值,您必须将它们解析为数值类型,例如双倍!

    【讨论】:

      【解决方案4】:

      使用 Convert.ToDecimal(txtSellingPrice.Text) >= Convert.ToDecimal(txtPurchasePrice.Text)

      字符串值不能这样比较

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-18
        • 1970-01-01
        • 1970-01-01
        • 2015-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多