473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Generic Method return Type

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
3 14970
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jim Newton | last post by:
hi all, i'm relatively new to python. I find it a pretty interesting language but also somewhat limiting compared to lisp. I notice that the language does provide a few lispy type nicities, but...
49
by: Steven Bethard | last post by:
I promised I'd put together a PEP for a 'generic object' data type for Python 2.5 that allows one to replace __getitem__ style access with dotted-attribute style access (without declaring another...
4
by: Charles Churchill | last post by:
I apologize if this question has been asked before, but after about half an hour of searching I haven't been able to find an answer online. My code is beloiw, with comments pertaining to my...
4
by: Andrew Ducker | last post by:
I have a collection of classes descending from a single root class (let's call it RootClass). They all currently have a property of Logical, of type Logical. However they actually return a...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
10
by: Egghead | last post by:
Hi all, Can someone kindly enough point me to some situations that we shall or "must" use Generic Class? I can foresee the Generic Method is powerful, but I can not find a single situation that...
2
by: ADN | last post by:
Hi, I have a method which calls my service factory: class person { public int ID { get; set: } public string Name {get; set;} } IList<Personmypeople = __serviceFactory.Fetch(new Person())
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
0
by: =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post by:
"Anders Borum" wrote: Hi Anders, I'm afraid the GetMethod() does not currently support filtering on generic parameters so you will have to loop through the existing methods using...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.