Printing a list of miscellaneous objects

I am trying to return a list of combinations of two different types of lists. Where all child classes are derived from the same base class, which has no code whatsoever. I can see items in the base class. But now that he was to have.

class BaseClass
{
   //empty no code
}
class ChildClassA : BaseClass
{

     public string Name {get; set;}
     public DateTime DateAccess {get; set;}

     public ChildClassA(string name, DateTime dateAccess){
        Name = name; 
        DateAccess = dateAccess;
     }


     public List<ChildClassA> GetListMethod()
     {
       //block of code

       return List<ChildClassA>;  // each items have Name and DateAccess
     }

}

class ChildClassB: BaseClass
{

     public string Group {get; set;}
     public DateTime SessionDuration {get; set;}
     public string Report{get; set;}

     public ChildClassB(string group, DateTime sessionDuration, string report)
     {
        Group = group; 
        SessionDuration = sessionDuration;
        Report = report; 
     }


   public List<ChildClassB> GetListMethod()
   {
     //block of code

    return List<ChildClassB>;   // each item has Group, SessionDuration and Report
   }
}

class CombineListClass
{
    public List<BaseClass> GetCombineList()
    {

        List<BaseClass> listFromBaseClass = new List<BaseClass>();

        ChildClassA a = new ChildClassA(); 
        var listFromA = a.GetListMethod();

        ChildClassB b = new ChildClassB(); 
        var listFromB = b.GetListMethod();

        listFromBaseClass.AddRange(listFromA);
        listFromBaseClass.AddRange(listFromB);

        foreach(var item in listFromBaseClass)
        {

           //print the item from each listFromBaseClass
           // needs to print Name, DateAccess, Group, SessionDuration, Report in one output file.

        }

   }
}

      

+3


source to share


2 answers


You should check the type of the object using the "is" keyword and cast it and display the correct values ​​accordingly:



 foreach(var item in listFromBaseClass)
    {

       //print the item from each listFromBaseClass
       // needs to print Name, DateAccess, Group, SessionDuration, Report in one output file.
        if (item is ChildClassA)
        {
              Console.WriteLine(((ChildClassA)item).Name);
        }
        else if (item is ChildClassB)
        {
              Console.WriteLine(((ChildClassB)item).Group);
        }
    }

      

+3


source


Each of them can have an interface implemented: IReportable

interface IReportable
{
    Row GenerateReportRow();
}

class ChildClassA : IReportable
{
    Row GenerateReportRow()
    {
        //whatever important items
    }
}

      

Then you can use the magic of polymorphism .



foreach(var item in listFromBaseClass)
{
   item.GenerateReportRow();
}

      

Otherwise, you can only have a collection of objects if they don't really share anything other than the need to store them in one place.

+3


source







All Articles