472,971 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,971 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 2136
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,...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.