473,804 Members | 3,462 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Desc ription:="Retur ns ArrayList")_
Public Function GetParticipants () As Participant()
<CUT>
adapter.Fill(cu stDS, "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("Participan tID")
.LastName = row("LastName") .ToString
.FirstName = row("FirstName" ).ToString
End With
arrList.Add(myP articipant)
Next

Dim arrParticipant As Participant() =
arrList.ToArray (GetType(Partic ipant))
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 2171
mojeza <na****@hotmail .comwrote in news:1178139077 .955623.103050
@e65g2000hsc.go oglegroups.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("Column 1")
MyObjectStore(" Column2").Value = DataRow("Column 2")
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("Column 1")
MyObjectStore(" Column2").Value = DataRow("Column 2")
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.goo glegroups.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").Valu e = DataRow("Column 1")
MyObjectStore( "Column2").Valu e = DataRow("Column 2")
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.Collecti ons.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
9115
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 takes a type and populates a collection then calls SetValue using reflection. How would I create the generic collection to fill it and call SetValue?
6
17185
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 BindingFlag that will only get the Generic one? Here is an example ... public class UserTest { public ArrayList GetCollection() { return new ArrayList(); }
5
3081
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
1834
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; namespace DeterminedGenericCollection { // I got tired of copy and pasting IDisposable
3
3680
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
3839
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 application that needs implementations in both Java and C#. I have the Java side done, and it works fantastic, and the C# side is nearly there. The problem I'm running into has to do with the differences in implementations of Generics between the two...
10
1941
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 I will need the Generic Class, given there are so many other options. -- cheers,
0
1184
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 maybe they were just renamed. Below is the code I have but cannot seem to get it to run in 2008. I keep getting error "The type or namespace name 'Directory' could not be found (are you missing a using directive or an assembly reference?" Any help...
7
1751
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 propInfos; private Type objectType = typeof(T); public SomeClass()
11
5392
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, fGetPerformanceGrade(SalesPersonID as Long) as String to retrieve that salesperson's grade (e.g. A+, A, A-, B+, B... D). Also one other function, fGetMonthlySales(SalesPersonID as Long) as String to get their sales figure, e.g. "$87K". I want web visitors to be able to click...
0
9711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10594
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10087
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9166
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6861
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.