473,503 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return a generic from a static function

Hi C# programmers,

I'd like to be able to call a static function or property and have it
return a generic list. The compiler indicates that "static types cannot
be used as generic arguments", so I'm looking for the correct way to do
this and/or other alternatives.

The static function should make some DB query, build some objects
foreach record and populate a List<T>. In the end, I'd like to be able
to use it like this:

foreach(Item item in MyListFunction) DoSomethingWith(item);

I'd like to do this without having to first create a List and fill it
with whatever, so behind MyListFunction is the DB query, List creation,
object building and filling of the List.
Thanks,
Bill
Oct 21 '08 #1
5 2160
"Bill McCormick" <wp*********@newsgroup.nospamwrote in message
news:u0**************@TK2MSFTNGP04.phx.gbl...
Hi C# programmers,

I'd like to be able to call a static function or property and have it
return a generic list. The compiler indicates that "static types cannot be
used as generic arguments", so I'm looking for the correct way to do this
and/or other alternatives.

The static function should make some DB query, build some objects foreach
record and populate a List<T>. In the end, I'd like to be able to use it
like this:

foreach(Item item in MyListFunction) DoSomethingWith(item);

I'd like to do this without having to first create a List and fill it with
whatever, so behind MyListFunction is the DB query, List creation, object
building and filling of the List.

It would really help if you specify which version of C# you are using and
what version of framework you are targeting. C#3 pointing at .NET 3.5 then
this sort of thing is best handled with LINQ.

In C#2 you are probably don't want to return a List<Tbut an Enumerable<T>
and you should be looking at the yeild return statement.

The fact that you can't use a Static type as a generic argument is not
relevent, you're not looking for a method to return a set of static types
(which is an oxymoron) you are looking for a static method to return a set
of instances of a type.

State versions for more detail.
--
Anthony Jones - MVP ASP/ASP.NET

Oct 21 '08 #2
Anthony Jones wrote:
"Bill McCormick" <wp*********@newsgroup.nospamwrote in message
news:u0**************@TK2MSFTNGP04.phx.gbl...
>Hi C# programmers,

I'd like to be able to call a static function or property and have it
return a generic list. The compiler indicates that "static types
cannot be used as generic arguments", so I'm looking for the correct
way to do this and/or other alternatives.

The static function should make some DB query, build some objects
foreach record and populate a List<T>. In the end, I'd like to be able
to use it like this:

foreach(Item item in MyListFunction) DoSomethingWith(item);

I'd like to do this without having to first create a List and fill it
with whatever, so behind MyListFunction is the DB query, List
creation, object building and filling of the List.


It would really help if you specify which version of C# you are using
and what version of framework you are targeting. C#3 pointing at .NET
3.5 then this sort of thing is best handled with LINQ.

In C#2 you are probably don't want to return a List<Tbut an
Enumerable<Tand you should be looking at the yeild return statement.

The fact that you can't use a Static type as a generic argument is not
relevent, you're not looking for a method to return a set of static
types (which is an oxymoron) you are looking for a static method to
return a set of instances of a type.

State versions for more detail.

I'm using C#2. I'll take a look at yield.

Thanks.
Oct 21 '08 #3
Bill McCormick wrote:
Anthony Jones wrote:
>"Bill McCormick" <wp*********@newsgroup.nospamwrote in message
news:u0**************@TK2MSFTNGP04.phx.gbl...
>>Hi C# programmers,

I'd like to be able to call a static function or property and have it
return a generic list. The compiler indicates that "static types
cannot be used as generic arguments", so I'm looking for the correct
way to do this and/or other alternatives.

The static function should make some DB query, build some objects
foreach record and populate a List<T>. In the end, I'd like to be
able to use it like this:

foreach(Item item in MyListFunction) DoSomethingWith(item);

I'd like to do this without having to first create a List and fill it
with whatever, so behind MyListFunction is the DB query, List
creation, object building and filling of the List.


It would really help if you specify which version of C# you are using
and what version of framework you are targeting. C#3 pointing at .NET
3.5 then this sort of thing is best handled with LINQ.

In C#2 you are probably don't want to return a List<Tbut an
Enumerable<Tand you should be looking at the yeild return statement.

The fact that you can't use a Static type as a generic argument is not
relevent, you're not looking for a method to return a set of static
types (which is an oxymoron) you are looking for a static method to
return a set of instances of a type.

State versions for more detail.

I'm using C#2. I'll take a look at yield.

Thanks.
Compiler still gives me the same error ("static types
>>cannot be used as generic arguments").
for example:

public static class MyObjControl {

private static IEnumerable<MyObjGetMyObjList() {
foreach(Foo foo in bar) {
MyObj myObj = new MyObj(foo);
yield return myObj;
}
}
}
Oct 21 '08 #4
On Oct 21, 5:51 pm, Bill McCormick <wpmccorm...@newsgroup.nospam>
wrote:
Bill McCormick wrote:
Anthony Jones wrote:
"Bill McCormick" <wpmccorm...@newsgroup.nospamwrote in message
news:u0**************@TK2MSFTNGP04.phx.gbl...
Hi C# programmers,
>I'd like to be able to call a static function or property and have it
return a generic list. The compiler indicates that "static types
cannot be used as generic arguments", so I'm looking for the correct
way to do this and/or other alternatives.
>The static function should make some DB query, build some objects
foreach record and populate a List<T>. In the end, I'd like to be
able to use it like this:
>foreach(Item item in MyListFunction) DoSomethingWith(item);
>I'd like to do this without having to first create a List and fill it
with whatever, so behind MyListFunction is the DB query, List
creation, object building and filling of the List.
It would really help if you specify which version of C# you are using
and what version of framework you are targeting. C#3 pointing at .NET
3.5 then this sort of thing is best handled with LINQ.
In C#2 you are probably don't want to return a List<Tbut an
Enumerable<Tand you should be looking at the yeild return statement.
The fact that you can't use a Static type as a generic argument is not
relevent, you're not looking for a method to return a set of static
types (which is an oxymoron) you are looking for a static method to
return a set of instances of a type.
State versions for more detail.
I'm using C#2. I'll take a look at yield.
Thanks.

Compiler still gives me the same error ("static types
>>cannot be used as generic arguments").

for example:

public static class MyObjControl {

private static IEnumerable<MyObjGetMyObjList() {
foreach(Foo foo in bar) {
MyObj myObj = new MyObj(foo);
yield return myObj;
}
}

}
Bill, I don't see anything wrong with code above. Would you point me
what breaks?

Thanks
Oct 21 '08 #5
puzzlecracker wrote:
On Oct 21, 5:51 pm, Bill McCormick <wpmccorm...@newsgroup.nospam>
wrote:
>Bill McCormick wrote:
>>Anthony Jones wrote:
"Bill McCormick" <wpmccorm...@newsgroup.nospamwrote in message
news:u0**************@TK2MSFTNGP04.phx.gbl...
Hi C# programmers,
I'd like to be able to call a static function or property and have it
return a generic list. The compiler indicates that "static types
cannot be used as generic arguments", so I'm looking for the correct
way to do this and/or other alternatives.
The static function should make some DB query, build some objects
foreach record and populate a List<T>. In the end, I'd like to be
able to use it like this:
foreach(Item item in MyListFunction) DoSomethingWith(item);
I'd like to do this without having to first create a List and fill it
with whatever, so behind MyListFunction is the DB query, List
creation, object building and filling of the List.
It would really help if you specify which version of C# you are using
and what version of framework you are targeting. C#3 pointing at .NET
3.5 then this sort of thing is best handled with LINQ.
In C#2 you are probably don't want to return a List<Tbut an
Enumerable<Tand you should be looking at the yeild return statement.
The fact that you can't use a Static type as a generic argument is not
relevent, you're not looking for a method to return a set of static
types (which is an oxymoron) you are looking for a static method to
return a set of instances of a type.
State versions for more detail.
I'm using C#2. I'll take a look at yield.
Thanks.
Compiler still gives me the same error ("static types
> >>cannot be used as generic arguments").

for example:

public static class MyObjControl {

private static IEnumerable<MyObjGetMyObjList() {
foreach(Foo foo in bar) {
MyObj myObj = new MyObj(foo);
yield return myObj;
}
}

}
Bill, I don't see anything wrong with code above. Would you point me
what breaks?

Thanks
See my previous post.

Thanks.
Oct 22 '08 #6

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

Similar topics

5
2738
by: Marijn | last post by:
I'd like to know how compilers usually handle static variables that are declared inside a function (as opposed to static class-members). Like in: int counter(){ static int c=0; ++c; return c;...
6
4439
by: aurgathor | last post by:
Howdy, How do I pass some function a generic comparison function? I figured out one non-generic case, but since this code got parameter declarations in two places, it's obviously not generic....
5
12158
by: rashmi | last post by:
how to use static function defined in one file in another file is that impposiible in 'c '
6
31461
by: Ravi | last post by:
Hi All: Is there any reason for declaring functions as static in a header file if that header file is going to be included in several other files? The compiler throws a warning for every such...
40
3178
by: vishnu | last post by:
Hi friend, i have a problem in my program what is the use of static function in C lang? plz help me
4
6932
by: msolem | last post by:
I have some code where there are a set of functions that return pointers to each other. I'm having a bit of a hard time figuring out the correct type to use to do that. The code below works but...
1
2110
by: Mark Ingram | last post by:
Hi, how can I use a static function in a web service from a C# app? I have the following code in a web service: public class MyClass { public static Boolean Exists(String check) { return...
4
5779
by: MichK | last post by:
Hello, I'm new in C#, and I'm working on some application to do direct audio recording and realtime plotting on screen. Now I'm having a lot of trouble to update the screen yet, it has been taking...
4
5296
by: dolphin | last post by:
Hi All I read a .cpp files,find that static void fun(void){......} int main() { .......... } What does this static function mean?Is it the same as the static
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.