It might be better to make MyName (or whatever specific stuff needs to be
done) abstract in Report.

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Brandon Willoughby
Sent: Friday, December 14, 2007 1:55 PM
To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
Subject: Re: [ADVANCED-DOTNET] Method that returns List<BaseClass>

I think that if you add a different kind of report in the GetReports()
method it might through an error though....

      public List<Report> GetReports()
      {
             List<Report> reports = new List<Report>();
             reports.Add(new Report());
             reports.Add(new ReportA());
             reports.Add(new ReportB());
             return reports;
      }

If the foreach loop is as you said:

foreach (ReportA item in reports)
     WL(item.MyName);

Then I think that the first two iterations would be fine, but the third
might break (on second thought, the first iteration might break too).
To be sure you could:

foreach(Report r in reports)
{
   if(r is ReportA) {
     // Do A-specific stuff
   }
   else if(r is ReportB) {
     // Do B-specific stuff
   }
}

I would test this but I am unable to at the moment.

Brandon


Ron Young wrote:
> Indeed it does. Thank you.
>
> I was under the impression that if the method that returned derived
reports ultimately returned a list of the base report, then the client would
get the properties defined in the base report.
>
> But it's up to the client to determine what types of reports are in the
list, so this works:
>
> // client-side
> List<Report> reports = provider.GetReports();
> foreach (ReportA item in reports)
>     WL(item.MyName);
>
> Thanks again.
>
> ________________________________________
> From: Discussion of advanced .NET topics.
[EMAIL PROTECTED] On Behalf Of Brandon Willoughby
[EMAIL PROTECTED]
> Sent: Friday, December 14, 2007 12:44 PM
> To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
> Subject: Re: [ADVANCED-DOTNET] Method that returns List<BaseClass>
>
> I believe if you change your List<ReportA> (inside of GetReports()) to
> type List<Report> it might work...
>
> Brandon
>
> Ron Young wrote:
>> Is the following possible? The compiler tells me no:
>>
>> class Report
>> {
>> }
>>
>> class ReportA : Report
>> {
>>      public string MyName = "ReportA";
>> }
>>
>> class ReportProvider
>> {
>>      public List<Report> GetReports()
>>      {
>>             List<ReportA> reports = new List<ReportA>();
>>             reports.Add(new Report());
>>             return reports;
>>      }
>> }
>>
>> I'd like to have that single method in ReportProvider, and hopefully a
jumptable to return different types of reports:
>>
>> class ReportProvider
>> {
>>        Dictionary<Type, Func<Criteria, List<Report>>> _mappings = new ...
>>
>>        public ReportProvider()
>>        {
>>                _mappings.Add(typeof(BillableHoursReport,
GetBillableHoursReport));
>>                _mappings.Add(typeof(DetailByDateReport,
GetDetailByDateReport));
>>        }
>>
>>        public IList<T> GetReport<T>(Criteria criteria) where T: Report
>>        {
>>                Type t = typeof(T);
>>                return _mappings[t].Invoke(criteria);
>>        }
>>        private IList<DetailByDateReport> GetDetailByDateReport(Criteria
criteria)
>>        {
>>                List<DetailByDateReport> results = new ...
>>                // do stuff
>>                return results;
>>        }
>>
>>        // other get report methods that return derived Report types
>> }
>>
>> Thanks
>>
>> ===================================
>> This list is hosted by DevelopMentor®  http://www.develop.com
>>
>> View archives and manage your subscription(s) at
http://discuss.develop.com
>
> ===================================
> This list is hosted by DevelopMentor®  http://www.develop.com
>
> View archives and manage your subscription(s) at
http://discuss.develop.com
>
> ===================================
> This list is hosted by DevelopMentor®  http://www.develop.com
>
> View archives and manage your subscription(s) at
http://discuss.develop.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to