473,395 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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
Aug 7 '08 #1
4 1104
>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.
Aug 7 '08 #2
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
Aug 7 '08 #3
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" <ma**********@gmail.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
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

Aug 8 '08 #4

"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:ef****************@TK2MSFTNGP03.phx.gbl...
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
Aug 8 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dan Holmes | last post by:
I have a class that i need a constraint of int, string, float or bool. I have tried the following but can't make VS accept it. I read the docs and they showed that any value type can be used...
4
by: Charles Churchill | last post by:
I apologize if this question has been asked before, but after about half an hour of searching I haven't been able to find an answer online. My code is beloiw, with comments pertaining to my...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
1
by: Néstor Sánchez A. | last post by:
Hi, is there a way, maybe using reflection, to use a generic class passing the type parameter dynamicly (not kwnowing the exact type at compile time)? I tried the next example, but doesn't work: ...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
6
by: Jon | last post by:
Hi, When I try to compile the following generic class, the compiler gives me many errors "Cannot conver type '...' to '...'" on the lines indicated. Besides, the C# compiler gives me errors even...
7
by: Henri.Chinasque | last post by:
Hi all, I've tried searching for this one, but can't seem to come up with the proper terms. I want to know how to pass a type to a generic method. Something like this: public void...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
2
by: Andrus | last post by:
Winforms UI assembly has static FormManager.FormCreator method which creates forms taking entity as parameter. I need to pass this method to business objects in business assembly so that business...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.