简体   繁体   中英

How do I fix the error 'Argument 1: cannot convert from 'string' to 'PassTask13_new.Customer' without using LINQ

What I'm trying to do is to let users to remove the customer account they want. Below are the values of 'customer account' I set in the main which the format is new Customer(_custName,_purchase,_status) from Customer class:

Customer[] customers = 
{
     new Customer("Mary", 20, Status.NonMember), 
     new Customer("Adam", 30, Status.Member),
     new Customer("Natasha", 50, Status.Member)
};

and this is the code I typed for remove method called in main:

Console.Write("\nEnter the customer's name that you want to remove: ");
string name = Console.ReadLine();

foreach(Customer c in a.CustomerList)
{
     if(a.CustomerList.Contains(name)) //error is at here the 'name'
     {
           a.DeleteCustAccount(c);
     }
}

I'm using Contain() to serach the matched customer name through the list and remove it because if I use index, user need to input '0' to remove the first 'customer account' it might be a bit confusing for some of them. I'm still new to C# programming and I'm not really sure how to fix this problem. Can anyone help an offer a suggestion?

You need to specify which property it should look for a match.

I tried to illustrate your problem and this piece of code should help.

class Customer
{
    public Customer(string custName, decimal purchase, Status status)
    {
        this.custName = custName;
        this.purchase = purchase;
        this.status = status;
    }
    public string custName { get; set; }
    public decimal purchase { get; set; }
    public Status status { get; set; }
}
enum Status
{
    Member, NonMember
}
class Program
{
    private static void Main()
    {

        Customer[] customers = new Customer[]
        {
            new Customer("Mary", 20, Status.NonMember),
            new Customer("Adam", 30, Status.Member),
            new Customer("Natasha", 50, Status.Member)
        };
        string name = "Mary";
        foreach (var c in customers)
        {
            if (c.custName.Contains(name))
            {
                Console.WriteLine("Do something");
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM