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

Return a generic from a static function

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.