2017-09-19 194 views
-6

我試圖調試此代碼,但在第三種方法中,它不允許我使用不同的數據類型。在我的教科書中,該示例顯示他們使用不同的數據類型作爲參數。爲什麼它不允許我這樣做?我確信這段代碼有其他問題,但是我直到清除錯誤時才調試它們。C#參數不能將int轉換爲字符串或字符串int

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")); 
     } 
    } 
} 
+3

C#是一種靜態類型的語言。無論你想要什麼,你都不能簡單地「使用不同的類型」。哪一行是拋出這個錯誤?你使用的是什麼類型與期望的類型?你想在這條線上完成什麼? – David

+0

第45行是我聲明方法的地方,第79行是我嘗試在字符串中使用三種數據類型的地方。格式 – Bob

+1

@Bob尼斯,讓我算你的帖子中的行:) –

回答

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); 
     } 
    } 

編輯:

我也忘了說,你可以替換與類解析器轉換,這避免了一個try/catch需要:

if (!Int32.TryParse(textBoxID.Text, out prodID)) 
{ 
    process = false; 
    // message method of choice to tell user of error 
} 
1

改變這一行

SearchProducts(prodID, prodName, prodPrice);

這一個

SearchProducts(prodName, prodID, prodPrice);

的SearchProducts方法預期字符串,int和double參數,但你通過一個int,字符串和雙。