2017-09-19 2 views
-6

이 코드를 디버깅하려고하지만 세 번째 방법에서는 다른 데이터 형식을 사용할 수 없습니다. 내 교과서에서이 예제는 다른 데이터 유형을 인수로 사용하는 예를 보여줍니다. 왜 그렇게 할 수 없습니까? 이 코드에는 다른 문제가 있다고 확신하지만 오류를 해결하기 전까지는 디버깅 할 수 없습니다.C# 인수가 int를 string 또는 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

Line 45는 메소드를 선언하는 곳입니다. Line 79는 string.format에서 세 가지 데이터 유형을 사용하려고 시도하는 곳입니다. – 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); 
     } 
    } 

편집 :

난 당신이 클래스 파서로 변환 대체 할 수있는 말을 잊지 않았다

,이 시도/잡기의 필요성 방지 :

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

변경이 줄이 하나

SearchProducts(prodName, prodID, prodPrice);

SearchProducts 방법은 문자열, INT 더블 매개 변수를 기대하지만 int로, 문자열을 건네

SearchProducts(prodID, prodName, prodPrice);

및 더블.

관련 문제