PDA

Click to See Complete Forum and Search --> : Generic Method Reutn Type


Bombdrop
Aug 2nd, 2007, 05:04 AM
Can any one help I have a method that will return a List to be bound as a datasource to a combobox see code for population below. I get the following error when i try to compile

Error 29 Cannot implicitly convert type 'System.Collections.Generic.List<Prsym.ComboPopulation.ComboInfo>' to 'System.Collections.Generic.List<T>'




/// <summary>
/// Gets the data source to be bound to a ComboBox.
/// </summary>
/// <param name="storedProcedure">The Name of the Stored Procedure to be used.</param>
/// <param name="scheme">The scheme.</param>
/// <returns>A Generic List to be dound to a ComboBox</returns>
private static List<T> GetDataSource<T>(string storedProcedure, PrsymScheme scheme)
{
Type dataSourceListType = typeof(T);

if (dataSourceListType == typeof(ComboInfo)) {
List<ComboInfo> dataSourceList = new List<ComboInfo>();
ComboInfo datSourceComboInfo = new ComboInfo();
DataBaseAccess DBA = new DataBaseAccess(scheme);
SqlDataReader reader = null;
DBA.LoadData(ref reader, storedProcedure);

while (reader.Read()) {
datSourceComboInfo.Display = reader["Display"].ToString();
datSourceComboInfo.Index = Convert.ToInt32(reader["Index"].ToString());
dataSourceList.Add(datSourceComboInfo);
}

//Clean up
reader.Dispose();

System.Collections.ArrayList p = new System.Collections.ArrayList();

return dataSourceList;

} else {
return null;
}

}



Any help would be greatly received

RudiVisser
Aug 2nd, 2007, 06:26 AM
By your original post title, I beleive the generic return method is object/Object, with the problem in your post, is there some sort of converter function from type 1 (System.Collections.Generic.List<Prsym.ComboPopulation.ComboInfo>) to type 2 (System.Collections.Generic.List<T>)?