Return a generic from a static function

B

Bill McCormick

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
 
A

Anthony Jones

Bill McCormick said:
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<T> but 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.
 
B

Bill McCormick

Anthony said:
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<T> but 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.
I'm using C#2. I'll take a look at yield.

Thanks.
 
B

Bill McCormick

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

Thanks.

Compiler still gives me the same error ("static types
for example:

public static class MyObjControl {

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

Bill McCormick

Bill said:
Compiler still gives me the same error ("static types

for example:

public static class MyObjControl {

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

Cancel that order; I got it. I mistakenly had something like
IEnumerable<MyObjControl>.

Thanks Again.
 
P

puzzlecracker

Compiler still gives me the same error ("static types

for example:

public static class MyObjControl {

private static IEnumerable<MyObj> GetMyObjList() {
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top