How to create generic object at run-time?

M

mojeza

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
 
G

Guest

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.
 
M

mojeza

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.
 
G

Guest

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/quickstart/howto/doc/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/quickstart/howto/doc/hashtable.aspx
 
M

mojeza

Please read up on arrays, lists, and collections:
http://samples.gotdotnet.com/quickstart/howto/doc/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/quickstart/howto/doc/hashtable.aspx

I tried hashtable, sortedlist but I get following error:
The type System.Collections.Hashtable is not supported because it
implements IDictionary
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top