2017-09-19 3 views
-6

J'essaie de déboguer ce code, mais dans la troisième méthode, il ne me permet pas d'utiliser des types de données différents. Dans mon manuel, l'exemple les montre en utilisant différents types de données en tant qu'argument. Pourquoi ne me permet-il pas de le faire? Je suis sûr que ce code a d'autres problèmes, mais je ne peux même pas les déboguer tant que je n'ai pas effacé les erreurs.C# L'argument ne peut pas convertir int en chaîne ou string en 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# est un langage typé statiquement. Vous ne pouvez pas simplement "utiliser des types différents" où vous voulez. Quelle ligne lance cette erreur? Quels sont les types que vous utilisez par rapport à ce qu'il attend? Qu'essayez-vous d'accomplir sur cette ligne? – David

+0

Ligne 45 est où je déclare la méthode, la ligne 79 est où j'essaie d'utiliser les trois types de données dans une chaîne.format – Bob

+1

@Bob Nice, laissez-moi compter les lignes dans votre message :) –

Répondre

1

Vous pouvez aussi utilement faire avec une erreur de manipulation sur la zone de texte conversions - il est jamais un bon plan pour prendre une zone de texte et attendez l'utilisateur de saisir simplement des données valides:

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

Modifier :

J'ai oublié de dire que vous pouvez remplacer Convertir les parseurs de classe, ce qui évite la nécessité d'un try/catch:

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

Modifier cette ligne

SearchProducts(prodID, prodName, prodPrice);

à celui-ci

SearchProducts(prodName, prodID, prodPrice);

La méthode recherchearticles attend String, int et paramètres doubles, mais vous avez passé un int, string et double.