Windows Vista Forums

Generic Class: Passing Data Type to Placeholder
  1. #1


    Etienne-Louis Nicolet Guest

    Generic Class: Passing Data Type to Placeholder

    I have a generic class defined as follows:

    Public Class SqlArgument(Of TDataType)

    Private _values as List(Of TDataType)

    Public Property Values() As List(Of TDataType)
    Get
    Return _values
    End Get
    Set(ByVal pValues As List(Of TDataType))
    _values = pValues
    End Set

    Public Sub New()
    Me.Values = New List(Of TDataType)
    End Sub

    ....

    End Class


    Given the DataType property of a (strongly typed) DataTable I'd like to
    create a new instance of SqlArgument with the data type of the appropriate
    table column. I thought about something like

    Sub TestArgument(pColumn as System.Data.DataColumn)
    ...
    Dim s As new SqlArgument(Of pColumn.DataType)
    ...
    End Sub



    but obviously this does not work. Can anybody give me a hint on how to do
    this?

    Many thanks for your suggestions,
    Etienne



      My System SpecsSystem Spec

  2. #2


    Mattias Sjögren Guest

    Re: Generic Class: Passing Data Type to Placeholder

    >Given the DataType property of a (strongly typed) DataTable I'd like to

    >create a new instance of SqlArgument with the data type of the appropriate
    >table column. I thought about something like
    >
    >Sub TestArgument(pColumn as System.Data.DataColumn)
    > ...
    > Dim s As new SqlArgument(Of pColumn.DataType)
    > ...
    >End Sub
    >
    >but obviously this does not work. Can anybody give me a hint on how to do
    >this?
    You have to go the Reflection route if you want to do this. You can
    instantiate the right generic type using Type.MakeGenericType and then
    an object from that type with Activator.CreateInstance. But know that
    once you've started using Reflection, it's hard to do something
    without it, and the code quickly gets pretty ugly.

    What are you actually going to do with the s variable once you've
    created the instance?


    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

      My System SpecsSystem Spec

  3. #3


    Marc Gravell Guest

    Re: Generic Class: Passing Data Type to Placeholder

    Can I just say: look at LINQ-to-SQL; that is exactly what it does for
    you. You might be able to save a lot of effort...

    Marc

      My System SpecsSystem Spec

  4. #4


    Etienne-Louis Nicolet Guest

    Re: Generic Class: Passing Data Type to Placeholder

    Dear Marc,

    I bought a book on LINQ yesterday and will have a look at it. Thanks for the
    tip!

    Kind regards,
    Etienne

    "Marc Gravell" <marc.gravell@xxxxxx> wrote in message
    news:%23$TZr1H%23IHA.2232@xxxxxx

    > Can I just say: look at LINQ-to-SQL; that is exactly what it does for you.
    > You might be able to save a lot of effort...
    >
    > Marc


      My System SpecsSystem Spec

  5. #5


    Etienne-Louis Nicolet Guest

    Re: Generic Class: Passing Data Type to Placeholder


    "Mattias Sjögren" <mattias.dont.want.spam@xxxxxx> wrote in message
    news:eftinBH%23IHA.3656@xxxxxx

    > >Given the DataType property of a (strongly typed) DataTable I'd like to
    >>create a new instance of SqlArgument with the data type of the appropriate
    >>table column. I thought about something like
    >>
    >>Sub TestArgument(pColumn as System.Data.DataColumn)
    >> ...
    >> Dim s As new SqlArgument(Of pColumn.DataType)
    >> ...
    >>End Sub
    >>
    >>but obviously this does not work. Can anybody give me a hint on how to do
    >>this?
    >
    > You have to go the Reflection route if you want to do this. You can
    > instantiate the right generic type using Type.MakeGenericType and then
    > an object from that type with Activator.CreateInstance. But know that
    > once you've started using Reflection, it's hard to do something
    > without it, and the code quickly gets pretty ugly.
    >
    > What are you actually going to do with the s variable once you've
    > created the instance?
    >
    >
    > Mattias
    >
    > --
    > Mattias Sjögren [C# MVP] mattias @ mvps.org
    > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    > Please reply only to the newsgroup.
    Dear Mattias,

    I tried to figure out how to use Reflection as recommended. I think i got
    the first part:

    Sub TestArgument(pColumn as System.Data.DataColumn)
    Dim generic As System.Type = GetType(SqlArgument(Of ))
    Dim typeArgs() As Type = {pColumn.DataType}
    Dim constructed As Type = generic.MakeGenericType(typeArgs)
    ....
    End Sub
    However, I did not get along with the VB help on Activator.CreateInstance.
    Would you mind giving me a hint how the code should look like?

    Many thanks & kind regards,
    Etienne



      My System SpecsSystem Spec

Generic Class: Passing Data Type to Placeholder problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
function cannot get a custom class by using add-type hayato PowerShell 2 01 Oct 2009
Is StringBuilder class Value type? sameem .NET General 2 18 Jul 2008
Passing credential object - what's the type? bobuva PowerShell 2 09 May 2008
Re: Unable to cast COM object of type 'ADODB.RecordsetClass' to class type 'System.Object[]' Michel Posseth [MCP] .NET General 2 27 Mar 2008
How to Create Collection Generic Class using new-object TonyDesweet PowerShell 6 01 Jul 2006