Generics - type casting

Discussion in 'ASP .Net' started by Random, Jun 4, 2007.

  1. Random

    Random Guest

    I want to define a generics method so the user can determine what type they
    expect returned from the method. By examining the generics argument, I
    would determine the operation that needs to be performed and do just that.
    However, out of the two possible ways of doing this, neither seems to work.

    I thought I could either...

    1) overload the method just based on the generics argument
    Public Function GetData (Of T As SqlDataReader) (ByRef command as
    SqlCommand) As T
    ...
    End Function

    Public Function GetData (Of T As DataSet) (ByRef command as SqlCommand) As T
    ...
    End Function

    ....or...

    2) determine the type without overloading and make sure to return the wanted
    type
    Public Function GetData (Of T) (ByRef command as SqlCommand) As T
    Select Case True
    Case TypeOf(T) Is GetType(SqlDataReader)
    Return command.ExecuteReader()
    Case TypeOf(T) Is GetType(DataSet)
    .....(other code)
    oDataAdapter.Fill(oDataSet)
    Return oDataSet
    End Select
    End Function

    But, #1 doesn't work because changing the generics type won't overload the
    function, and #2 doesn't work because I get errors along the lines of "Value
    of type '...' cannot be converted to 'T'"

    I understand #1 not working, but as long as I'm playing it safe with the
    type conversions, why can't I get #2 to work?
    Random, Jun 4, 2007
    #1
    1. Advertising

  2. Might I suggest

    Public Function GetDataSet(ByVal command As SqlCommand) As DataSet

    and

    Public Function GetDataReader(ByVal command As SqlCommand) As
    SqlDataReader


    If you use a generic method in this situation the user will always
    have to specify the type with the method anyways, so building the type
    into the method name is just as easy to use.

    In short, this situation does not sufficiently benefit from using
    Generics.

    If you really want to do your #2, you need to add a DirectCast call to
    get around the implicit conversion error.

    HTH,

    Sam


    ------------------------------------------------------------
    We're hiring! B-Line Medical is seeking .NET
    Developers for exciting positions in medical product
    development in MD/DC. Work with a variety of technologies
    in a relaxed team environment. See ads on Dice.com.


    On Mon, 4 Jun 2007 09:17:18 -0700, "Random" <>
    wrote:

    >I want to define a generics method so the user can determine what type they
    >expect returned from the method. By examining the generics argument, I
    >would determine the operation that needs to be performed and do just that.
    >However, out of the two possible ways of doing this, neither seems to work.
    >
    >I thought I could either...
    >
    >1) overload the method just based on the generics argument
    >Public Function GetData (Of T As SqlDataReader) (ByRef command as
    >SqlCommand) As T
    > ...
    >End Function
    >
    >Public Function GetData (Of T As DataSet) (ByRef command as SqlCommand) As T
    > ...
    >End Function
    >
    >...or...
    >
    >2) determine the type without overloading and make sure to return the wanted
    >type
    >Public Function GetData (Of T) (ByRef command as SqlCommand) As T
    > Select Case True
    > Case TypeOf(T) Is GetType(SqlDataReader)
    > Return command.ExecuteReader()
    > Case TypeOf(T) Is GetType(DataSet)
    > .....(other code)
    > oDataAdapter.Fill(oDataSet)
    > Return oDataSet
    > End Select
    >End Function
    >
    >But, #1 doesn't work because changing the generics type won't overload the
    >function, and #2 doesn't work because I get errors along the lines of "Value
    >of type '...' cannot be converted to 'T'"
    >
    >I understand #1 not working, but as long as I'm playing it safe with the
    >type conversions, why can't I get #2 to work?
    >
    >
    Samuel R. Neff, Jun 4, 2007
    #2
    1. Advertising

  3. Random

    Random Guest

    Thanks, Sam. Actually, being able to use the same method name will help
    with the implementation part of what I'm doing.

    And, I'd already tried both CType and DirectCast - still won't convert to
    'T'.

    I just tried a third way to fix this, and apparently if I specify Object as
    the return type, I'm okay. Odd.

    "Samuel R. Neff" <> wrote in message
    news:...
    >
    > Might I suggest
    >
    > Public Function GetDataSet(ByVal command As SqlCommand) As DataSet
    >
    > and
    >
    > Public Function GetDataReader(ByVal command As SqlCommand) As
    > SqlDataReader
    >
    >
    > If you use a generic method in this situation the user will always
    > have to specify the type with the method anyways, so building the type
    > into the method name is just as easy to use.
    >
    > In short, this situation does not sufficiently benefit from using
    > Generics.
    >
    > If you really want to do your #2, you need to add a DirectCast call to
    > get around the implicit conversion error.
    >
    > HTH,
    >
    > Sam
    >
    >
    > ------------------------------------------------------------
    > We're hiring! B-Line Medical is seeking .NET
    > Developers for exciting positions in medical product
    > development in MD/DC. Work with a variety of technologies
    > in a relaxed team environment. See ads on Dice.com.
    >
    >
    > On Mon, 4 Jun 2007 09:17:18 -0700, "Random" <>
    > wrote:
    >
    >>I want to define a generics method so the user can determine what type
    >>they
    >>expect returned from the method. By examining the generics argument, I
    >>would determine the operation that needs to be performed and do just that.
    >>However, out of the two possible ways of doing this, neither seems to
    >>work.
    >>
    >>I thought I could either...
    >>
    >>1) overload the method just based on the generics argument
    >>Public Function GetData (Of T As SqlDataReader) (ByRef command as
    >>SqlCommand) As T
    >> ...
    >>End Function
    >>
    >>Public Function GetData (Of T As DataSet) (ByRef command as SqlCommand) As
    >>T
    >> ...
    >>End Function
    >>
    >>...or...
    >>
    >>2) determine the type without overloading and make sure to return the
    >>wanted
    >>type
    >>Public Function GetData (Of T) (ByRef command as SqlCommand) As T
    >> Select Case True
    >> Case TypeOf(T) Is GetType(SqlDataReader)
    >> Return command.ExecuteReader()
    >> Case TypeOf(T) Is GetType(DataSet)
    >> .....(other code)
    >> oDataAdapter.Fill(oDataSet)
    >> Return oDataSet
    >> End Select
    >>End Function
    >>
    >>But, #1 doesn't work because changing the generics type won't overload the
    >>function, and #2 doesn't work because I get errors along the lines of
    >>"Value
    >>of type '...' cannot be converted to 'T'"
    >>
    >>I understand #1 not working, but as long as I'm playing it safe with the
    >>type conversions, why can't I get #2 to work?
    >>
    >>

    >
    Random, Jun 4, 2007
    #3
  4. But if you specify Object as a return type then there's really no
    point in using Generics....

    Was the error different when you tried using DirectCast ?

    Sam

    ------------------------------------------------------------
    We're hiring! B-Line Medical is seeking .NET
    Developers for exciting positions in medical product
    development in MD/DC. Work with a variety of technologies
    in a relaxed team environment. See ads on Dice.com.


    On Mon, 4 Jun 2007 09:50:15 -0700, "Random" <>
    wrote:

    >Thanks, Sam. Actually, being able to use the same method name will help
    >with the implementation part of what I'm doing.
    >
    >And, I'd already tried both CType and DirectCast - still won't convert to
    >'T'.
    >
    >I just tried a third way to fix this, and apparently if I specify Object as
    >the return type, I'm okay. Odd.
    >
    Samuel R. Neff, Jun 4, 2007
    #4
  5. Random

    Random Guest

    Nope. No difference in error when using DirectCast.

    By passing in the type (I also found out I could just pass in Type as a
    parameter and that works, too), I can discover how my method needs to
    perform.

    "Samuel R. Neff" <> wrote in message
    news:...
    >
    > But if you specify Object as a return type then there's really no
    > point in using Generics....
    >
    > Was the error different when you tried using DirectCast ?
    >
    > Sam
    >
    > ------------------------------------------------------------
    > We're hiring! B-Line Medical is seeking .NET
    > Developers for exciting positions in medical product
    > development in MD/DC. Work with a variety of technologies
    > in a relaxed team environment. See ads on Dice.com.
    >
    >
    > On Mon, 4 Jun 2007 09:50:15 -0700, "Random" <>
    > wrote:
    >
    >>Thanks, Sam. Actually, being able to use the same method name will help
    >>with the implementation part of what I'm doing.
    >>
    >>And, I'd already tried both CType and DirectCast - still won't convert to
    >>'T'.
    >>
    >>I just tried a third way to fix this, and apparently if I specify Object
    >>as
    >>the return type, I'm okay. Odd.
    >>

    >
    Random, Jun 4, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. heyo
    Replies:
    3
    Views:
    887
    Dan Pop
    Apr 1, 2004
  2. pete
    Replies:
    4
    Views:
    784
    Dan Pop
    Apr 2, 2004
  3. William
    Replies:
    5
    Views:
    1,002
  4. William
    Replies:
    3
    Views:
    638
    Andreas Leitgeb
    Mar 4, 2011
  5. William
    Replies:
    16
    Views:
    458
Loading...

Share This Page