Link to home
Start Free TrialLog in
Avatar of CipherIS
CipherISFlag for United States of America

asked on

C# Implement Generics on Export Class

Trying to create an export using Generics.  Most of it works except one piece of code.  Basically I want to pass the model to the export and perform the export in the class.  Here is what I have (error is indicated below).
private void Form1_Load(object sender, EventArgs e)
{
    SQLData<PersonModel> sqldata = new SQLData<PersonModel>();
    List<PersonModel> personModel = sqldata.SelectSPROC("Get_Persons");
    ExportPersons.PerformExport<PersonModel>();
}

Open in new window

public static class ExportPersons
{
    public static void PerformExport<T>()
    {
        ExportExcel<T> exportExcel = new ExportExcel<T>();
        exportExcel.ExportData<T>();
    }
}

Open in new window

    public class ExportExcel<T> : IExportData<T>
    {
        public void ExportData<T>()
        {
            var model = List<T>();  //HERE IS THE ERROR - 'System.Collections.Generic.List<T>' is a 'type' but is used like a 'variable'
        }
    }

Open in new window

    public interface IExportData<T>
    {
        void ExportData<T>();  //HERE - Type parameter 'T' has the same name as the type parameter for the outer type.
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CipherIS

ASKER

Normal Interfaces?  Please elaborate.  Reason I'm using generic is because I want to pass different models.
That worked.  Had to make some changes to the code but it worked.  Wish I had Resharper on this computer.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have no compiler at hands right now, but this should be the outline:

public interface IModel<T> { IEnumerable<T> GetAll(); }
public interface IExportData { void ExportData(IModel<T> model); }

public class Persons: IModel<Person> {
    // ToDo: Implement GetAll().
}

public class ExportExcel: IExportData
{
    public void ExportData(IModel<T> model)
    {
        List<T> entities = model.GetAll();
    }
}

public static class ExportPersons
{
    public static void PerformExport(IModel<Person> model)
    {
        ExportExcel exportExcel = new ExportExcel();
        exportExcel.ExportData(model);
    }
}

private void Form1_Load(object sender, EventArgs e)
{
    Persons persons = new Persons();
    ExportPersons.PerformExport(persons);
}

Open in new window