Return a generic from a static function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill McCormick

    #1

    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
  • Anthony Jones

    #2
    Re: Return a generic from a static function

    "Bill McCormick" <wpmccormick@ne wsgroup.nospamw rote in message
    news:u0sDfz7MJH A.4380@TK2MSFTN GP04.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

    Comment

    • Bill McCormick

      #3
      Re: Return a generic from a static function

      Anthony Jones wrote:
      "Bill McCormick" <wpmccormick@ne wsgroup.nospamw rote in message
      news:u0sDfz7MJH A.4380@TK2MSFTN GP04.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.

      Comment

      • Bill McCormick

        #4
        Re: Return a generic from a static function

        Bill McCormick wrote:
        Anthony Jones wrote:
        >"Bill McCormick" <wpmccormick@ne wsgroup.nospamw rote in message
        >news:u0sDfz7MJ HA.4380@TK2MSFT NGP04.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(Ite m 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<Tan d 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<MyO bjGetMyObjList( ) {
        foreach(Foo foo in bar) {
        MyObj myObj = new MyObj(foo);
        yield return myObj;
        }
        }
        }


        Comment

        • puzzlecracker

          #5
          Re: Return a generic from a static function

          On Oct 21, 5:51 pm, Bill McCormick <wpmccorm...@ne wsgroup.nospam>
          wrote:
          Bill McCormick wrote:
          Anthony Jones wrote:
          "Bill McCormick" <wpmccorm...@ne wsgroup.nospamw rote in message
          >news:u0sDfz7MJ HA.4380@TK2MSFT NGP04.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<MyO bjGetMyObjList( ) {
          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

          Comment

          • Bill McCormick

            #6
            Re: Return a generic from a static function

            puzzlecracker wrote:
            On Oct 21, 5:51 pm, Bill McCormick <wpmccorm...@ne wsgroup.nospam>
            wrote:
            >Bill McCormick wrote:
            >>Anthony Jones wrote:
            >>>"Bill McCormick" <wpmccorm...@ne wsgroup.nospamw rote in message
            >>>news:u0sDfz7 MJHA.4380@TK2MS FTNGP04.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(Ite m 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.
            >>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<MyO bjGetMyObjList( ) {
            > 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.

            Comment

            Working...