Click here to Skip to main content
15,901,426 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to do an assignment on a making a distribution company that adds and removes goods from their ledger. I cannot do the remove part as in the remove function when I pass if(foodstring.Contains(foodRemover)) the foodRemover gives out an error that it cannot convert string to House_Goods.Product.Food<string>. Thanks!

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace House_Goods
{
    class Product : Food, Cleaner
    {
        public string food { get; set; }
        public string cleaner { get; set; }

        public string foodRemover { get; set; }
        public string cleanerRemover { get; set; }
        
        public class Food<t>
        {
            public T food { get; set; }
            public T doFood(T f)
            {
                return f;
                

            }
        }
        public class Cleaner<t>
        {
            public T cleaner { get; set; }
            public T doCleaner(T c)
            {
                return c;

            }
        }
        List<food<string>> foodstring = new List<food<string>>();
        List<food<int>> foodint = new List<food<int>>();
        List<cleaner<string>> cleanstring = new List<cleaner<string>>();
        List<cleaner<int>> cleanint = new List<cleaner<int>>();
        public void addFood() 
        {
            
            Console.WriteLine("Food:");
           
            
            Food<string> food1 = new Food<string>();
              
            Console.Write(food1.doFood("Pizza"));
            foodstring.Add(food1);
            
            Food<int> food1index = new Food<int>();
            Console.Write(food1index.doFood(1));
            
            Console.WriteLine();
            Food<string> food2 = new Food<string>();
            Console.Write(food2.doFood("Pasta"));
          
            Food<int> food2index = new Food<int>();
            Console.Write(food2index.doFood(2));
            
            Console.WriteLine();
            Food<string> food3 = new Food<string>();
            Console.Write(food3.doFood("Sushi"));
           
            Food<int> food3index = new Food<int>();
            Console.Write(food3index.doFood(3));
            
            Console.WriteLine();
            Console.WriteLine("======================");
        }
        public void removeFood()
        {
            
            Console.WriteLine("Enter food you want to remove");
            foodRemover = Console.ReadLine();
            if(foodstring.Contains(foodRemover))
            {
                foodstring.Remove(foodRemover);
            }
            else
            {
                Console.WriteLine("Food not found");
            }


        }
        public void addCleaner()
        {
            Console.WriteLine("Cleaners:");
            Cleaner<string> cleaner1 = new Cleaner<string>();
            Console.Write(cleaner1.doCleaner("Baking soda"));
            
            Cleaner<int> cleanerindex1 = new Cleaner<int>();
            Console.Write(cleanerindex1.doCleaner(1));
            
            Console.WriteLine();
            Cleaner<string> cleaner2 = new Cleaner<string>();
            Console.Write(cleaner2.doCleaner("Mouth wash"));
           
            Cleaner<int> cleanerindex2 = new Cleaner<int>();
            Console.Write(cleanerindex2.doCleaner(2));
    
            Console.WriteLine();
            Cleaner<string> cleaner3 = new Cleaner<string>();
            Console.Write(cleaner3.doCleaner("Toothpaste"));
        
            Cleaner<int> cleanerindex3 = new Cleaner<int>();
            Console.Write(cleanerindex3.doCleaner(3));
          
            Console.WriteLine();
        }
        public void removeCleaner()
        {
            Console.WriteLine("Enter food name you want to remove");
            cleanerRemover = Console.ReadLine();
            if (cleanstring.Contains(cleanerRemover))
            {
                cleanstring.Remove(cleanerRemover);
            }
            else
            {
                Console.WriteLine("Cleaner not found");
            }
        }
    }
}


What I have tried:

I've tried specifying the types in the lists. Also I have tried to solve this problem elsewhere in the code but it just keeps popping up in another place as I change.
Posted
Updated 11-Oct-22 18:14pm
v2
Comments
Member 8428760 11-Oct-22 23:28pm    
Print cleanerRemover and make sure it is not getting set to null.

1 solution

Look at your code:
C#
public string foodRemover { get; set; }
...
List<food<string>> foodstring = new List<food<string>>();
C#
foodRemover = Console.ReadLine();
if(foodstring.Contains(foodRemover))

foodRemover is a string; foodstring is a collection of food instances.
So when you try to call Contains, the system tries to find a way to cast the string to a food and does not find any implicit converter and throws the error. It will do the same for your cleaner code.

That whole code looks like you don't really understand classes and inheritance very well yet - it can't work the way you are trying to do it because your Product class is trying to derive from both the Food and Cleaner classes, and that's wrong - C# will not allow you to derive from more than one base class.
Even if it could, you are saying that a Product is both a Food and a Cleaner, rather than a Cleaner is a Product; and a Food is a product.
In real sword terms, that's is saying that this single piece of fruit in my hand is both an Apple and a Bicycle because Walmart sell both items!

Product is the base class: both Food and Cleaner should derive from that.

Make a copy of that code so you can look back on it in a few weeks time, then throw it all away - it's fundamentally wrong, and none of it is worth keeping.
Then go back to your course notes and read up on classes and inheritance again before re-reading your assignment. You need to work out exactly what you are doing before you leap into code!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900