467,895 Members | 1,472 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,895 developers. It's quick & easy.

How to create generic object at run-time?

I would like to create generic object which will be used for store of
single row of DataTable. Lets say I create class as follow:

Public Class Participant
Public ParticipantID As Int64
Public LastName As String
Public FirstName As String
End Class

then I get a data from the database to DataTable and create array of
objects typed as Participant:

<WebMethod(Description:="Returns ArrayList")_
Public Function GetParticipants() As Participant()
<CUT>
adapter.Fill(custDS, "myData")
Dim arrList As New ArrayList
For Each row As DataRow In custDS.Tables("myData").Rows
Dim MyParticipant As New Participant
With myParticipant
.ParticipantID = row("ParticipantID")
.LastName = row("LastName").ToString
.FirstName = row("FirstName").ToString
End With
arrList.Add(myParticipant)
Next

Dim arrParticipant As Participant() =
arrList.ToArray(GetType(Participant))
Return arrParticipant
End Function

What I like to accomplish is to create a generic class which could be
use to accomodate data pull from any DataTable with various column
numbers. In another words, when DataTable is populated I would like to
enumarate a column collection and create as many items in my generic
class as there are column in DataTable. Can this be done?

Just to clarify, this is WebService function which is consume by Adobe
Flex 2 client. This function works very well with Flex 2 but I like to
write more generic one which would be able to return any set of rows
from any SELECT query.

Thanks in advance,
John

May 2 '07 #1
  • viewed: 1952
Share:
4 Replies
mojeza <na****@hotmail.comwrote in news:1178139077.955623.103050
@e65g2000hsc.googlegroups.com:
What I like to accomplish is to create a generic class which could be
use to accomodate data pull from any DataTable with various column
numbers. In another words, when DataTable is populated I would like to
enumarate a column collection and create as many items in my generic
class as there are column in DataTable. Can this be done?
You can already enumerate the a datarow object (Columns is a collection).

If you like to create something generic, you'll need to store each piece of
data as a element in a collection, i.e.:
MyObjectStore("Column1").Value = DataRow("Column1")
MyObjectStore("Column2").Value = DataRow("Column2")
etc.

Otherwise if you like to create a truly dynamic type... I believe you can
do this with reflection. However, I don't believe web services can handle
that sort of object ... since you need the WSDL defintion beforehand.
May 2 '07 #2
If you like to create something generic, you'll need to store each piece of
data as a element in a collection, i.e.:

MyObjectStore("Column1").Value = DataRow("Column1")
MyObjectStore("Column2").Value = DataRow("Column2")
etc.
Could you please be more specific as to how I must declare
MyObjectStore (what type):
Dim MyObjectStore As ????

and how to dimension MyObjectStore in order to accomodate all colums
in a given DataRow.

Thank you.

May 2 '07 #3
mojeza <na****@hotmail.comwrote in
news:11**********************@n59g2000hsh.googlegr oups.com:
>If you like to create something generic, you'll need to store each
piece of data as a element in a collection, i.e.:

MyObjectStore("Column1").Value = DataRow("Column1")
MyObjectStore("Column2").Value = DataRow("Column2")
etc.

Could you please be more specific as to how I must declare
MyObjectStore (what type):
Dim MyObjectStore As ????

and how to dimension MyObjectStore in order to accomodate all colums
in a given DataRow.
Please read up on arrays, lists, and collections:

http://samples.gotdotnet.com/quickst...c/default.aspx

Collections:
Iterate over a collection?
Use a Hashtable collection?
Choose a collection to use?
Implement a collection?
Clone a collection?

In particular, Hashtables could be used in your circumstance:

http://samples.gotdotnet.com/quickst...hashtable.aspx
May 2 '07 #4
Please read up on arrays, lists, and collections:
>
http://samples.gotdotnet.com/quickst...c/default.aspx

Collections:
Iterate over a collection?
Use a Hashtable collection?
Choose a collection to use?
Implement a collection?
Clone a collection?

In particular, Hashtables could be used in your circumstance:

http://samples.gotdotnet.com/quickst...hashtable.aspx
I tried hashtable, sortedlist but I get following error:
The type System.Collections.Hashtable is not supported because it
implements IDictionary

May 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Paul Welter | last post: by
6 posts views Thread by Paul Welter | last post: by
8 posts views Thread by JAL | last post: by
3 posts views Thread by kim.nolsoee | last post: by
13 posts views Thread by rkausch | last post: by
10 posts views Thread by Egghead | last post: by
11 posts views Thread by Mark B | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.