Generic Method return Type

B

BombDrop

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Change the signature of the method to

private static IList GetDataSource<T>(string storedProcedure, PrsymScheme
scheme)

It should work the same
 
B

Burrows

Thanks for the reply Ignacio Machin but found the anwaser is was the
way I was casting the return

#region Class Header
////////////////////////////////////////////////////////////////////////////////
//NameSpace :GenericTest
//Class Name :GenericReturnTest
////////////////////////////////////////////////////////////////////////////////
//Copyright :© Albion Software
//Date :03/08/2007
//Author :© SBurrows
//Purpose :A Class showing an example of how a Generic Method
// : with a Generic Return Type works.
////////////////////////////////////////////////////////////////////////////////
#endregion Class Header

using System;
using System.Collections.Generic;
namespace GenericTest
{

#region ComboInfo Struct
/// <summary>
/// A structure for the population of Comboboxes or Listboxes
/// </summary>
struct ComboInfo
{
string display;
int index;

/// <summary>
/// Gets or sets the display.
/// </summary>
/// <value>The display.</value>
public string Display
{
get
{
return display;
}
set
{
display = value;
}
}


/// <summary>
/// Gets or sets the index.
/// </summary>
/// <value>The index.</value>
public int Index
{
get
{
return index;
}
set
{
index = value;
}
}
}
#endregion


#region ComboInfoWithComments Struct
/// <summary>
/// A structure for the population of Comboboxes or Listboxes
/// </summary>
struct ComboInfoWithComments
{
string display;
int index;
string comments;

/// <summary>
/// Gets or sets the comments.
/// </summary>
/// <value>The comments.</value>
/// <Date>02/08/2007</Date>
/// <Author> SBurrows</Author>
public string Comments
{
get
{
return comments;
}
set
{
comments = value;
}
}


/// <summary>
/// Gets or sets the display.
/// </summary>
/// <value>The display.</value>
public string Display
{
get
{
return display;
}
set
{
display = value;
}
}


/// <summary>
/// Gets or sets the index.
/// </summary>
/// <value>The index.</value>
public int Index
{
get
{
return index;
}
set
{
index = value;
}
}
}
#endregion ComboInfoWithComments Struct
/// <summary>
/// Description of Test.
/// </summary>
public class GenericReturnTest
{
public GenericReturnTest()
{

//Call the test Method using ComboInfo as Type
List<ComboInfo> info = TestGen<ComboInfo>();
System.Windows.Forms.MessageBox.Show(info[0].Display);

//Call the test Method using ComboInfoWithComments as Type
List<ComboInfoWithComments> infoWithCom =
TestGen<ComboInfoWithComments>();
System.Windows.Forms.MessageBox.Show(infoWithCom [1].Display);
System.Windows.Forms.MessageBox.Show(infoWithCom [1].Comments);


}

/// <summary>
/// Method that returs a generic List
/// </summary>
/// <returns>A List<T></returns>
public List<T> TestGen<T>()
{
//Get Type of to be returned
Type tp= typeof(T);

if(tp == typeof(ComboInfo)){

List<ComboInfo> dataSourceList = new List<ComboInfo>();
ComboInfo info = new ComboInfo();
info.Display ="Stephen";
info .Index =66;
dataSourceList.Add(info);
info = new ComboInfo();
info.Display="Hello Form NO Comments";
info.Index=31;
dataSourceList.Add(info);

return dataSourceList as List<T>;

}else if (tp == typeof(ComboInfoWithComments)){
List<ComboInfoWithComments> dataSourceList = new
List<ComboInfoWithComments>();
ComboInfoWithComments info = new ComboInfoWithComments();
info.Display ="Generics";
info .Index =66;
info.Comments="Generics";
dataSourceList.Add(info);
info = new ComboInfoWithComments();
info.Display="Hello Form With Comments";
info.Index=31;
info.Comments="With Comments are working";
dataSourceList.Add(info);

return dataSourceList as List<T>;
}else{
return null;
}
}

}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Thanks for the reply Ignacio Machin but found the anwaser is was the
way I was casting the return



Yes, that is similar, I prefer the use of IList as you can return any kind
of list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top