473,327 Members | 2,090 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,327 software developers and data experts.

Calling a generic method that has a return value

I've been wrestling with this for a while and can't figure it out.

I have a generic method with the following signature:
//this compiles OK
public abstract class DataMethod
{
public abstract T Select<T>();
}

I want to call it like this:
public override T Select<T>()
{
......
}

But when I compile I get various types of compile errors depending on how I try
to do the code in the overridden method.

Here is what I have tried:

public override T Select<T>()
{
T = new Dictionary<string, DxccCountry>();
}
Error 1 'T' is a 'type parameter' but is used like a 'variable'

I've tried all the ways I know of to get this to work.

T t = new Dictionary<string, DxccCountry>();

Error 1 Cannot implicitly convert type
'System.Collections.Generic.Dictionary<string,N5GE .HamLib.Core.DxccCountry>' to
'T'

Is it not possible to use a generic method with a return type without passing it
into the method as a parameter?
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Jan 14 '07 #1
3 1729
Otis Mukinfus wrote:
I have a generic method with the following signature:
public abstract class DataMethod
{
public abstract T Select<T>();
}

I want to call it like this:

public override T Select<T>()
Yep, that looks right. When you override a generic method, you override
it for all values of the type parameters.
But when I compile I get various types of compile errors depending on how I try
to do the code in the overridden method.
public override T Select<T>()
{
T = new Dictionary<string, DxccCountry>();
}
Error 1 'T' is a 'type parameter' but is used like a 'variable'
You can't assign to a type parameter. T receives a value sent by the
caller. What if someone polymorphically calls DataMethod.Select<int>()?
How would that work when you've tried to assign this Dictionary<,to T?
Is it not possible to use a generic method with a return type without passing it
into the method as a parameter?
There seems to be a faulty assumption underlying what you're saying
there. The problem has nothing to do with return types. The type
arguments are always passed in by the caller. The callee (Select<in
your case) doesn't get to choose the value of T. Tha caller does.

-- Barry

--
http://barrkel.blogspot.com/
Jan 14 '07 #2
Consider your line:
T t = new Dictionary<string, DxccCountry>();

Now; if <Tis <int>, this reads:
int t = new Dictionary<string, DxccCountry>();
(and then presumably "return t").

Which clearly isn't going to work. Only valid casts are legal. If you
know it will cast at runtime, sometimes you need to cast (/box) to
object and back for this to work. However, the above clearly never will
work.

If you mean to return a dictionary, then perhaps something like:

Dictionary<string, Tdict = new Dictionary<string, T>()

etc. Other options include adding "where" clauses etc to the type of T,
or using interfaces etc. If you can expand on what you want, we can
help clarify.

Marc

Jan 14 '07 #3
On 14 Jan 2007 14:39:38 -0800, "Marc Gravell" <ma**********@gmail.comwrote:
>Consider your line:
T t = new Dictionary<string, DxccCountry>();

Now; if <Tis <int>, this reads:
int t = new Dictionary<string, DxccCountry>();
(and then presumably "return t").

Which clearly isn't going to work. Only valid casts are legal. If you
know it will cast at runtime, sometimes you need to cast (/box) to
object and back for this to work. However, the above clearly never will
work.

If you mean to return a dictionary, then perhaps something like:

Dictionary<string, Tdict = new Dictionary<string, T>()

etc. Other options include adding "where" clauses etc to the type of T,
or using interfaces etc. If you can expand on what you want, we can
help clarify.

Marc

Thanks, Guys.

I think I must be taking the wrong approach to doing what I want.

I have a class named CountryTable:

public class CountryTable
{
....
}

Which I want to use to call stored procedures from a database for a table Named
"CountryTable". I want to have methods in the class name Select, Update, Delete
and Insert.

The reason I am trying to use generics for this is to reduce the amount of code
I have to write, since I have many more tables that I want to access in this
manner.

I could use overloaded methods to do this but if I make a Select method with the
following signature:
public Dictionary<string, DxccCountrySelect();

I have used up my overloads for that method.

I was hoping I could use a generic return to avoid passing a parameter to avoid
compiler errors. I could do this to accomplish the overloading:
public Dictionary<string, DxccCountrySelect(Dictionary<string, DxccCountry>
countries)
{
// do some stuff
return countries;
}

and then :
public List<DxccCountrySelect(List<DxccCountrycountries)
{
//do some stuff
return countries;
}

IMHO that's not good practice.

I guess my trouble here is not knowing how to implement the abstract method from
the DataMethod class.

What I was hoping to end up with was something like this in the caller:

CountryTable tbl = new CountryTable();

Dictionary<string, DxccCountrycountries = tbl.Select();

Obviously I missed the mark somewhere on the way ;o)

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Jan 14 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
1
by: jens Jensen | last post by:
Hello , i'm calling a webservice generated with oracle webservice java tools. I'm not able to add a web reference to a .net client the usual way with visual studio 2005. I was therefore...
5
by: Metaman | last post by:
I'm trying to write a generic method to generate Hashcodes but am having some problems with (generic) collections. Here is the code of my method: public static int GetHashCode(object input) {...
2
by: D2 | last post by:
Hi, I have a requirement where I need to call a generic method without knowing the class name i.e. I'm getting class name from xml file. Given below is a replica of the scenario we are having. ...
8
by: Jeff | last post by:
Still new to vb.net in VS2005 web developer... What is the proper/standard way of doing the following - setting the value of a variable in one sub and calling it from another? E.g., as below....
2
by: Dragan | last post by:
Hi, We're working in VS 2005, Team edition, if it makes any difference at all (should be up-to-date and all that, but could not guarantee it is 100%). We've implemented a simple generic wrapper...
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...
3
by: BombDrop | last post by:
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 ...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.