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"));
}
}
}
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
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
@Bob Nice, laissez-moi compter les lignes dans votre message :) –