Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Generic Class: Passing Data Type to Placeholder

Reply
 
Old 08-07-2008   #1 (permalink)
Etienne-Louis Nicolet


 
 

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
Old 08-07-2008   #2 (permalink)
Mattias Sjögren


 
 

Re: Generic Class: Passing Data Type to Placeholder

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

>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
Old 08-07-2008   #3 (permalink)
Marc Gravell


 
 

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
Old 08-08-2008   #4 (permalink)
Etienne-Louis Nicolet


 
 

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
Quote:

> 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
Old 08-08-2008   #5 (permalink)
Etienne-Louis Nicolet


 
 

Re: Generic Class: Passing Data Type to Placeholder


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

> >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
Reply

Thread Tools


Similar Threads
Thread Forum
Is StringBuilder class Value type? .NET General
Passing credential object - what's the type? PowerShell
Re: Unable to cast COM object of type 'ADODB.RecordsetClass' to class type 'System.Object[]' .NET General
How to Create Collection Generic Class using new-object PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46