473,219 Members | 1,551 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,219 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 14966
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: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.