views:

37

answers:

1

I have a class which uses a list for passing to ssrs and for each report I had to define a list of that type. like this

List<ReportClass1> ReportClass1prods;
public List<ReportClass1> GetReportClass1
{
    get
    {
        return ReportClass1prods;
    }
}
List<ReportClass2> ReportClass2prods;
public List<ReportClass2> GetReportClass2
{
    get
    {
        return ReportClass2prods;
    }
}

List<ReportClass3> ReportClass3prods;
public List<ReportClass3> GetReportClass3
{
    get
    {
        return ReportClass3prods;
    }
}

and then each one has its own function

public void LoadReport1(List<ReportClass1> products)
{
    ReportClass1prods = products;
    reportViewer1.Clear();
    reportViewer1.Reset(); 
    reportViewer1.ProcessingMode = ProcessingMode.Local;
    reportViewer1.LocalReport.ReportPath = "Report1.rdlc";
    reportViewer1.LocalReport.DataSources.Add(
            new ReportDataSource("ReportClass1", GetReportClass1));
    reportViewer1.RefreshReport();
}

Is there anyway/ what is the best way to make it so I dont need to copy and paste each list function for each new report but can dynamically figure out the type so there is only 1 list and 1 load function?

A: 

Check which common class or interface are ReportClass1,2,3 derived from (or if these are your classes, not some kind of generated ones, make it derive from a common base class or interface). Then use List<BaseClass>, e.g. List<ReportClass>.

František Žiačik
Also, what is the purpose of having those list properties? Do you really need them along with the LoadReport function? You could pass the "products" parameter directly to new ReportDataSource() instead of calling GetReportClass.Also, please follow the naming recommendations - use lower first letter for private members and Upper first letter for public properties and let the names match, e.g:List<ReportClass> reportClassProducts;public List<ReportClass> ReportClassProducts { ... }
František Žiačik
They are custom classes, from what I am understanding you are suggesting-- public class ReportClass1 : myReportClass{}and then pass in List<myReportClass>???
Philip
Well, yes. But even it is not necessary: you could make your LoadReport function look like this: LoadReport(IEnumerable products) or even LoadReport(object products), as according to this http://msdn.microsoft.com/en-us/library/ms251736(v=VS.80).aspx ReportDataSource constructor accepts an object as datasource as long as it implements IEnumerable, IDataSource or is a BindingSource. That way you can pass in any List<> you want.
František Žiačik