By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,537 Members | 1,750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,537 IT Pros & Developers. It's quick & easy.

Generic Method return Type

P: n/a
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.ComboPopula tion.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<TGetDataSource<T>(string
storedProcedure, PrsymScheme scheme)
{
Type dataSourceListType = typeof(T);

if (dataSourceListType == typeof(ComboInfo)) {
List<ComboInfodataSourceList = 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

Aug 2 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,

Change the signature of the method to

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

It should work the same

"BombDrop" <bu*************@gmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
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.ComboPopula tion.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<TGetDataSource<T>(string
storedProcedure, PrsymScheme scheme)
{
Type dataSourceListType = typeof(T);

if (dataSourceListType == typeof(ComboInfo)) {
List<ComboInfodataSourceList = 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

Aug 2 '07 #2

P: n/a
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>
/// <AuthorSBurrows</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<ComboInfoinfo = TestGen<ComboInfo>();
System.Windows.Forms.MessageBox.Show(info[0].Display);

//Call the test Method using ComboInfoWithComments as Type
List<ComboInfoWithCommentsinfoWithCom =
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<TTestGen<T>()
{
//Get Type of to be returned
Type tp= typeof(T);

if(tp == typeof(ComboInfo)){

List<ComboInfodataSourceList = 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<ComboInfoWithCommentsdataSourceList = 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;
}
}

}
}
Aug 3 '07 #3

P: n/a
Hi,

"Burrows" <bu*************@gmail.comwrote in message
news:11*********************@19g2000hsx.googlegrou ps.com...
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.
Aug 3 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.