0

I have several classes

public class A { public string A1; public string A2; public string A3; }
public class B { public string B1; public string B2; }
public class C { public string C1; public string C2; public string C3; public string C4; }

And a dataSource class

public class DataSource
{
    [Description("A")]
    public List<A> LstA;

    [Description("B")]
    public List<B> LstB;

    [Description("C")]
    public List<C> LstC;
}

Here is what I want to do , read a string with "A" "B" "C", If I read an "A", Then I create a table withDataSource.LstAwhich has three columns A1 A2 A3.If I read a "B", Then I create a table whith DataSource.LstBwhich has two columns B1 B2 and so on.

Here is how I made it

public DataSet CreateTablesFromString(char[] lstchar)
    {
        DataSource ds = new DataSource();
        DataSet dataset = new DataSet();
        for(int i = 0; i < lstchar.Length; i++)
        {
            if(lstchar[i] == 'A')
            {
                dataset.Tables.Add(CreateTable(ds.LstA));
            }
            else if(lstchar[i] == 'B')
            {
                dataset.Tables.Add(CreateTable(ds.LstB));
            }
            else if(lstchar[i] == 'C')
            {
                dataset.Tables.Add(CreateTable(ds.LstC));
            }
        }
        return dataset;
    }

    private DataTable CreateTable<T>(List<T> lstData)
    {
        PropertyInfo[] lstinfo = lstData.ElementAt(0).GetType().GetProperties();
        int rowCount = lstData.Count;
        int columnCount = lstinfo.Length;
        DataTable dt = new DataTable();
        for(int i = 0; i < columnCount; i++)
        {
            dt.Columns.Add();
        }
        for(int i = 0; i < rowCount; i++)
        {
            object[] row = new object[columnCount];
            for(int j = 0; j < columnCount; j++)
            {
                row[j] = lstinfo[j].GetValue(lstData[i]);
            }
        }
        return dt;
    }

My problem is : Is there any way to avoid so many "if else"

if(lstchar[i] == 'A')
        {
            dataset.Tables.Add(CreateTable(ds.LstA));
        }
        else if(lstchar[i] == 'B')
        {
            dataset.Tables.Add(CreateTable(ds.LstB));
        }
        else if(lstchar[i] == 'C')
        {
            dataset.Tables.Add(CreateTable(ds.LstC));
        }

Is there a way to figure out which list I will use without "ifelse". Maybe use [Description] attritude? But I don't know how.

edjoker
  • 157
  • 1
  • 14
  • Maybe using reflection to check the [Description] attribute value. LstA, LstB are properties in DataSource, like you get the properties in CreateTable function. See this question https://stackoverflow.com/questions/6637679/reflection-get-attribute-name-and-value-on-property – Alin Aug 31 '21 at 12:53
  • Have you thought about using generics for your table creation? https://stackoverflow.com/questions/68273081/abstract-database-access/68273797#68273797 – ChrisBD Aug 31 '21 at 14:10
  • @James I know how to figure out which list I want with [Description] attribute.I just cannot avoid using "ifelse" when I using the CreateTable method – edjoker Sep 01 '21 at 08:56
  • https://stackoverflow.com/questions/232535/how-do-i-use-reflection-to-call-a-generic-method/232621#232621 – edjoker Sep 02 '21 at 06:55

0 Answers0