简体   繁体   中英

C# Error CS1503 Argument 1: cannot convert from 'string' to 'Character'

The problem is that I keep getting this error

Cannot convert from OmzetApplicatie.lib.Category to string

The error occurs on this line of code:

categories.Add(new Category(item));

Complete code:

List<string> categories;

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    string[] categorynames = { "Vlees", "Snacks", "Drank", "Groenten en Fruit", "Droge voeding", "Sauzen", "Diepvries" };

    foreach (string item in categorynames)
    {
        categories.Add(new Category(item));
    }

    Product[][] products = {
            new Product[] { new Product("gehakt", 6.4M, 5.30M), new Product("hamburger", 9.0M, 6.25M), new Product("kip", 9.7M, 7.13M), new Product("biefstuk", 17.9M, 13.38M), new Product("preparé", 10.3M, 7.86M) },
            new Product[] { new Product("chips", 1.8M, 1.51M), new Product("nootjes", 7.00M, 5.83M), new Product("suikerwafels", 0.9M, 0.73M), new Product("snoep", 1.9M, 1.34M) },
            new Product[] { new Product("water", 0.3M, 0.23M), new Product("cola", 2.0M, 1.55M), new Product("ice-tea", 1.8M, 1.23M), new Product("fanta", 1.8M, 1.34M), new Product("sprite", 1.8M, 1.36M) },
            new Product[] { new Product("appels", 1.2M, 1.03M), new Product("komkommer", 0.4M, 0.36M), new Product("paprika", 2.4M, 2.05M), new Product("banaan", 1.4M, 1.15M), new Product("champignons", 0.8M, 0.63M) },
            new Product[] { new Product("spaghetti", 1.3M, 1.05M), new Product("rijst", 2.2M, 2.03M), new Product("hamburgerbroodjes", 2.4M, 2.17M), new Product("brood", 1.4M, 1.03M), new Product("macaroni", 0.3M, 0.26M) },
            new Product[] { new Product("ketchup", 2.7M, 2.27M), new Product("mayonaise", 1.7M, 1.12M), new Product("pesto", 0.8M, 0.63M), new Product("tomatensaus", 0.5M, 0.38M) },
            new Product[] { new Product("ijs", 1.7M, 1.34M), new Product("frieten", 1.7M, 1.62M), new Product("kroketten", 3.2M, 3.05M), new Product("frikandel", 1.9M, 1.62M), new Product("pizza", 2.3M, 2.16M) }
    };

    for (int i = 0; i < products.Length; i++)
    {
        foreach (Product p in products[i])
        {
            categories[i].AddProduct(p);
        }
    }
}

This is my class from my class library

namespace OmzetApplicatie.lib
{
    public class Category
    {
        public string item;
        public string[] categorynames;

        public Category(string item)
        {
            this.item = item;
        }
    }
}

I think it should be

 List<Category> categories;

If you change

List<string> categories;

to

List<Category> categories = new List<Category>();

at least you will be able to compile.

Whether that is what you wanted remains to be seen.

You need to write as below:

categories.Add(new Category(item).item);

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