我的逻辑是错误的

时间:2015-06-14 12:33:20

标签: c#

这应该很简单,但事实并非如此。消息框显示产品数量和产品总价的零。不太清楚为什么。这是迄今为止的代码:

产品页面

//will add the qty of the tshirts to the shop page. will catch non-integers
protected void linkTShirtAdd_Click(object sender, EventArgs e)
{
    int intOutput = 0;
    lblTShirtWarning.Visible = false;

    //determine that the value is parsable - if it is, assign values. Else, display error
    if (int.TryParse(txtTShirtQty.Text, out intOutput))
    {
        ProductClass.productName = "T-Shirt";
        ProductClass.productPrice = 10;
        ProductClass.productQty = Int16.Parse(txtTShirtQty.Text);
        //price of tshirts
        int totalTShirtPrice = ProductClass.productPrice * ProductClass.productQty;
        //display summary of the order
        MessageBox.Show (new Form {TopMost = true},
            "ORDER REVIEW" + "\n_______________________\n"
            + ProductClass.productName + "\n" 
            + "Quantity: " + ProductClass.productQty +"\n" 
            + "Total Price: " + totalTShirtPrice);
        //Response.Redirect("./Shop.aspx");
    }
    else
    {
        lblTShirtWarning.Visible = true;
        lblTShirtWarning.Text = "Please enter a valid number";
        txtTShirtQty.Text = "";
    }
}

产品类别(存储值)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for ProductClass
/// </summary>
public static class ProductClass
{

//obvious variables
private static String product;
private static int price;
private static int quantity;

//get and set the name of the product
public static String productName
{
    get
    {
        return product;
    }
    set
    {
        product = value;
    }
}

//get and set the price of the product
public static int productPrice
{
    get
    {
        return price;
    }
    set
    {
        price = value;
    }
}

//get and set the quantity of the product
public static int productQty
{
    get
    {
        return quantity;
    }
    set
    {
        price = value;
    }
}


}

1 个答案:

答案 0 :(得分:3)

ProductCode.productQty的getter和setter使用不同的支持字段:

 public static int productQty
 {
     get
     {
         return quantity;
     }
     set
     {
         price = value;
     }
 }

显然,在设置器中,price = value;是错误的,你可能意味着quantity = value;