Home > Net >  How could I iterate through list of abstract type, containing non-abstract types derived from that a
How could I iterate through list of abstract type, containing non-abstract types derived from that a

Time:08-19

I need to iterate through list I created, but can't access objects inside. I tried a few different functions but nothing worked and I'm afraid I'm using the wrong tools for the job.

namespace WholesaleApp
{
 
    internal class Program : Wholesale
    {
        static string filePath = "C:\\Wholesale.txt";
       
        static void Main(string[] args)
        {
            List<Merchandise> productList = new List<Merchandise>();

            productList = ReadFromFile(filePath);
            foreach(Merchandise merchandise in productList)
            {
                //this is where I'm trying to access objects inside and display them
            }
        }
    }
}

This is my abstract class:

namespace WholesaleApp
{
    internal abstract class Merchandise
    {
        public string merchandiseName { get; set; }
        public int merchandiseAmount { get; set; }

        public Merchandise(string name, int amount)
        {
            merchandiseName = name;
            merchandiseAmount = amount;
        }
    }
}

And this is one of the three classes deriving from Merchandise abstract class:

namespace WholesaleApp
{
    internal class MerchandiseClothing : Merchandise
    {

        public string clothSize { get; set; }
        public string clothType { get; set; }

        public MerchandiseClothing(string _clothType, string _clothSize, string name, int amount) : base(name, amount)
        {
            clothType = _clothType;
            clothSize = _clothSize;
        }
        
        public void ReturnAll()
        {
            Console.Write(merchandiseName " of type: " clothSize " in amount: " merchandiseAmount  " and jeep status is: " clothType);
        }
    }
}

Finally, my function where I add everything to the final list:

namespace WholesaleApp
{
    internal class Wholesale 
    {
        static public List<Merchandise> ReadFromFile(string filePath)
        {
        List<Merchandise> result = new List<Merchandise>();

        string line;
        StreamReader reader = null!;
            try
            {
                reader = new StreamReader(filePath);
                
                line = reader.ReadLine()!;
                
                while (line != null)
                {
                    string[] words = line.Split(';');
                    if (words[0] == "MerchandiseComputer")
                    {
                        result.Add(new MerchandiseComputer(words[1], words[2], Int32.Parse(words[3])));
                    }
                    else if (words[0] == "MerchandiseCarParts")
                    {
                        result.Add(new MerchandiseCarParts(bool.Parse(words[1]), words[3], words[2], Int32.Parse(words[4])));
                    }
                    else if (words[0] == "MerchandiseClothing")
                    {
                        result.Add(new MerchandiseClothing(words[1], words[2], words[3], Int32.Parse(words[4])));
                    }

                    line = reader.ReadLine()!;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                reader.Close();
            }

            return result;
        }
    }
}

CodePudding user response:

It should be possible to iterate here iterate through objects already. If you want to use specific fields from each specific class, you can put here a check on type and do whatever you want. For example:

foreach (Merchandise merchandise in productList)
{
    if (merchandise is MerchandiseClothing clothing)
    {
        Console.WriteLine(clothing.clothSize); //Can be use any field from Clothing class 
        Console.WriteLine(clothing.merchandiseAmount); //And also from parent
    }
    else if (merchandise is MerchandiseComputer computer)
    {
        //Do what you want
    }
}

However, better to make abstract method like WriteToConsole in Merchandise class and override it in each implementation. Like ReturnAll method in your MerchandiseClothing class

  •  Tags:  
  • c#
  • Related