oop - How to declare a dictionary compatible with any type in C# -
i have created abstract base class parent of other classes. have class database connection m fetching records , wanted records converted objects of class pass function. have written function ..
enter code here //class appconnection.cs public static dictionary<int, baseclass> fetchobjects( string sqlquery, baseclass obj ) { datatable dt = myconnection.fetchrecords( sqlquery ); dictionary<int,baseclass> objdict = new dictionary<int, baseclass>(); for(int i=0; < dt.rows.count; i++ ) { obj.mapobjectbydatarow(dt.rows[i]); //abstract function in baseclass objdict.add(obj.id, obj); } return objdict; }
i calling in child classes this,
enter code here class customer:baseclass { public static dictionary<int, customer> fetchcustomers() { string sql = "select * customers"; dictionary<int, customer> dict = appconnection.fetchobjects(sql, new customer() ); return dict; } }
this gives error cannot convert implicitly
enter code here dictionary<int,baseclass> dictionary<int,customer>
i think want generic method?
public static dictionary<int,t> fetchobjects<t>(string sqlquery, t obj) t : baseclass { datatable dt = myconnection.fetchrecords( sqlquery ); dictionary<int,t> objdict = new dictionary<int, t>(); for(int i=0; < dt.rows.count; i++ ) { obj.mapobjectbydatarow(dt.rows[i]); //abstract function in baseclass objdict.add(obj.id, obj); } return objdict; }
Comments
Post a Comment