CS1503参数1:无法转换为'字符串'到' int'
这是我得到的错误。
private void btnCalculate_Click(object sender, EventArgs e)
{
string strMailingLabel;
try
{
//Create an instance of clsCustomer using the overloaded constructor
clsCustomer cobjCustomer = new clsCustomer(txtName.Text, txtStreet.Text,
txtCity.Text, txtState.Text, txtZip.Text);
strMailingLabel = cobjCustomer.Name + "\n" +
cobjCustomer.Street + "\n" +
cobjCustomer.City + ", " +
cobjCustomer.State + " " + cobjCustomer.Zip;
//Display mailing address
lblMailingLabel.Text = strMailingLabel;
//Create an instance of clsOrder using the overloaded constructor
clsOrder cobjOrder = new clsOrder
(txtDescription.Text, //Error is Here
int.Parse(txtQuantity.Text),
decimal.Parse(txtPrice.Text));
cobjOrder.calcExtendedPrice();
cobjOrder.accumulateTotals();
lblExtension.Text = cobjOrder.ExtendedPrice.ToString("C");
lblTotalCount.Text = clsOrder.TotalCount.ToString("N0");
lblTotalPrice.Text = clsOrder.TotalPrice.ToString("C");
}
这是订单代码
public clsOrder()
{
}
public clsOrder(int intQuantity, decimal decPrice, decimal decDescription)
{
this.Quantity = intQuantity;
this.Price = decPrice;
this.Description = decDescription;
}
//declare property methods
public int Quantity
{
get
{
return cintQuantity;
}
set
{
cintQuantity = value;
}
}
public decimal Price
{
get
{
return cdecPrice;
}
set
{
cdecPrice = value;
}
}
public decimal Description
{
get
{
return cdecDescription;
}
set
{
cdecDescription = value;
}
}
我将描述设置为小数,我知道这是我做错了,问题是我不知道如何正确编码。有人有想法吗?
答案 0 :(得分:1)
你订的错了。试试这个
clsOrder cobjOrder = new clsOrder(
Convert.ToInt32(txtQuantity.Text),
Convert.ToDecimal(txtPrice.Text),
Convert.ToDecimal(txtDescription.Text));
答案 1 :(得分:0)
//Create an instance of clsOrder using the overloaded constructor
clsOrder cobjOrder = new clsOrder
(txtDescription.Text, //Error is Here
int.Parse(txtQuantity.Text),
decimal.Parse(txtPrice.Text));
在查看构造函数时似乎没有问题:
public clsOrder(int intQuantity, decimal decPrice, decimal decDescription)
{
this.Quantity = intQuantity;
this.Price = decPrice;
this.Description = decDescription;
}
需要int, decimal, decimal
,您的输入为string, int, decimal
以int
开头,然后需要2 decimals
。
所以在我看来你不小心输入了错误的输入。
您需要的是:
//Create an instance of clsOrder using the overloaded constructor
clsOrder cobjOrder = new clsOrder
(int.Parse(txtQuantity.Text), //Error is Here
decimal.Parse(txtPrice.Text)),
txtDescription.Text);
public clsOrder(int intQuantity, decimal decPrice, string decDescription)
{
this.Quantity = intQuantity;
this.Price = decPrice;
this.Description = decDescription;
}
另请确保将Description
更改为string
!
编辑:正如有些人建议的那样,您可能想要使用TryParse
。有很多关于如何使用它的信息。那不是你的主要问题,所以我不会用它来捣蛋。