我试图调试此代码,但在第三种方法中,它不允许我使用不同的数据类型。在我的教科书中,该示例显示了它们使用不同的数据类型作为参数。为什么不允许我这样做?我确定此代码还有其他问题,但我甚至无法调试它们,直到我清除错误。
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 WP_Week3_Problem2
{
public partial class Problem2 : Form
{
public Problem2()
{
InitializeComponent();
}
int[] productID = { 1234, 5678, 5677, 8765, 2345, 8734 };
double[] productPrice = { 5.55, 20.0, 40.0, 56.34, 78.10, 98.0 };
String[] productName = { "Pencil", "Backpack", "MessengerBag", "RoomHeater", "SewingMachine", "Decorative Lamp" };
private void Problem2_Load(object sender, EventArgs e)
{
}
private void buttonLoadTable_Click(object sender, EventArgs e)
{
labelOutput.Text = "ProdID Price ProdName";
for (int i = 0; i < productID.Length; i = i + 1)
{
labelOutput.Text += String.Format("\n {0} {1} {2} ", productID[i], productPrice[i].ToString("C"), productName[i]);
}
}
private void buttonSearch_Click(object sender, EventArgs e)
{
int prodID = Convert.ToInt32(textBoxID.Text);
String prodName = textBoxName.Text;
double prodPrice = Convert.ToDouble(textBoxPrice.Text);
SearchProducts(prodID);
SearchProducts(prodName);
SearchProducts(prodID, prodName, prodPrice);
}
private void SearchProducts(int prodID)
{
int prodMinus = prodID - 1;
int idExists = Array.IndexOf(productID, prodMinus);
if (idExists == -1)
{
labelOutput.Text = "That product ID is invalid.\nPlease enter a valid product ID";
textBoxID.Text = "";
}
}
private void SearchProducts(String prodName)
{
String nameExists = Array.IndexOf(productName, prodName).ToString();
bool isName = true;
if (nameExists == "Pencil")
isName = true;
else if (nameExists == "Backpack")
isName = true;
else if (nameExists == "MessengerBag")
isName = true;
else if (nameExists == "RoomHeater")
isName = true;
else if (nameExists == "SewingMachine")
isName = true;
else if (nameExists == "Decorative Lamp")
isName = true;
else
isName = false;
labelOutput.Text = "This Product Name is invalid.\nPlease enter a valid Product Name";
}
private void SearchProducts(String prodName, int prodID, double prodPrice)
{
labelOutput.Text = String.Format("Your Product ID of {0} is a {2} and it costs: {1}", prodID, prodName, prodPrice.ToString("C"));
}
}
}
答案 0 :(得分:1)
更改此行
SearchProducts(prodID, prodName, prodPrice);
到这个
SearchProducts(prodName, prodID, prodPrice);
SearchProducts方法需要String,int和double参数,但是你传递了一个int,string和double。
答案 1 :(得分:1)
您还可以对文本框转换中的某些错误处理进行有用的操作 - 从不是一个很好的计划来获取文本框并希望用户只需输入有效数据:
private void buttonSearch_Click(object sender, EventArgs e)
{
bool process = true;
String prodName = textBoxName.Text;
int prodID;
double prodPrice;
try
{
prodID = Convert.ToInt32(textBoxID.Text);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error in product ID", MessageBoxButtons.OK, MessageBoxIcon.Error);
process = false;
}
try
{
prodPrice = Convert.ToDouble(textBoxPrice.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error in product price", MessageBoxButtons.OK, MessageBoxIcon.Error);
process = false;
}
if (process)
{
SearchProducts(prodID);
SearchProducts(prodName);
SearchProducts(prodID, prodName, prodPrice);
}
}
编辑:
我忘了说你可以用类解析器替换Convert,这样就不需要try / catch了:
if (!Int32.TryParse(textBoxID.Text, out prodID))
{
process = false;
// message method of choice to tell user of error
}