Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How could I iterate through list of abstract type, containing non-abstract types derived from that abstract class?

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 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 class deriving from Merchandise abstract class

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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, sorry for the long block but i think it may be important!

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;

        }

    }
}

Sorry if my formating is off – that is my first question here and i really tried to make it readable! If something is wrong, please let me know:)

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading