473,408 Members | 1,734 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,408 software developers and data experts.

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
4 2155
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Welter | last post by:
Is there anyway to do the following? Type myType = typeof(User); Collection<myType> list = new Collection<myType>(); I know I could just use User instead of myType but have a function that...
6
by: Paul Welter | last post by:
I'm trying to get a method using Type.GetMethod. There are two methods with that same name, one is a standard method, the other is a Generic method. How do I get the Generic method? Is there a...
5
by: cody | last post by:
I know I can use ArrayList.ReadOnly() to create a readonly version of it, but how can I achive a similar thing with a generic collection of .NET 2.0?
8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
3
by: kim.nolsoee | last post by:
Hi I want to use the Dictionary Classs that will load my own class called KeyClass used as TKey. Here is the code: public class Dictionary { public static void Main()
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
10
by: Egghead | last post by:
Hi all, Can someone kindly enough point me to some situations that we shall or "must" use Generic Class? I can foresee the Generic Method is powerful, but I can not find a single situation that...
0
by: mradam | last post by:
Good morning. Could someone please assist me in find the correct way to do this. I have tried a couple methods but it seems VS 2008 does not support all the directives that previous versions did, or...
7
by: tadmill | last post by:
Is it possible for a class that accepts a generic type, to re-create another instance of itself with a property type of the passed class? ex. public class SomeClass<T> { private PropertyInfo...
11
by: Mark B | last post by:
I want to display a pre-designed graphical 'performance badge' on certain webpages (round, about 2cm diameter) next to a salesperson's details. I have a function,...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.