PC Review


Reply
Thread Tools Rate Thread

Dynamic Class Properties from Data Row Columns

 
 
Brent
Guest
Posts: n/a
 
      20th Sep 2005
I can't seem to locate any easy way to create dynamic class properties.
Essentially, I want to take the column headers from a data result set,
create a property having the column header name, and then set the value
of the property to the data in the corresponding field.

For example, if I had this row from table "users":



ID Name Age

1 Barry 35
2 Brenda 33

....I could then write a SQL statement, e.g., "SELECT * from users where
ID = 2"

....and then pass it through a class

Users myuser = new Users();

that would loop through the column names, creating the properties Name
and Age (right there!) and then setting the property value by looping
through the values. Accessing the "Users" class would yield...

Users.Name = Brenda
Users.Age = 33

What I'm trying to avoid is typing out 100 column header names, and then
having to add or subtract more names as the database changes...also, I
could create classes out of any-ol'-table much easier.

I'd sure appreciate some help!

--Brent
 
Reply With Quote
 
 
 
 
Robbe Morris [C# MVP]
Guest
Posts: n/a
 
      20th Sep 2005
You could in .NET but it would be way too slow at runtime.
Check out the source code for the object mapper below.
It creates the classes for you at design time. A much faster
approach.

--
Robbe Morris - 2004/2005 Microsoft MVP C#
Free Source Code for ADO.NET Object Mapper To DataBase Tables And Stored
Procedures
http://www.eggheadcafe.com/articles/..._generator.asp




"Brent" <"writeBrent at the google mail place"> wrote in message
news:(E-Mail Removed)...
>I can't seem to locate any easy way to create dynamic class properties.
>Essentially, I want to take the column headers from a data result set,
>create a property having the column header name, and then set the value of
>the property to the data in the corresponding field.
>
> For example, if I had this row from table "users":
>
>
>
> ID Name Age
>
> 1 Barry 35
> 2 Brenda 33
>
> ...I could then write a SQL statement, e.g., "SELECT * from users where ID
> = 2"
>
> ...and then pass it through a class
>
> Users myuser = new Users();
>
> that would loop through the column names, creating the properties Name and
> Age (right there!) and then setting the property value by looping through
> the values. Accessing the "Users" class would yield...
>
> Users.Name = Brenda
> Users.Age = 33
>
> What I'm trying to avoid is typing out 100 column header names, and then
> having to add or subtract more names as the database changes...also, I
> could create classes out of any-ol'-table much easier.
>
> I'd sure appreciate some help!
>
> --Brent



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to handle dynamic Array properties in a class module? deltaquattro Microsoft Excel Programming 2 15th Mar 2010 01:24 PM
Editing data containing dynamic properties Andrus Microsoft C# .NET 0 18th Jan 2009 02:03 PM
Accessing class properties with dynamic name Amy Blankenship Microsoft Access Form Coding 0 5th Dec 2007 08:51 PM
create class with dynamic properties davidw Microsoft C# .NET 1 20th Jul 2006 11:18 PM
ArrayList of custom class with dynamic properties not displayed correctly in DataGrid raymi Microsoft C# .NET 2 15th Jul 2005 09:03 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:15 PM.