Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Members

Status
Not open for further replies.

tempname

Programmer
May 15, 2008
6
US
I am working on an app where I require the use of dynamic members. In my app, I have the query customer that has several fields I want to be able to pull data from. Ideally I could do this statically, but I will need to use some of my previous classes to add more queries. So that is why I decided to do this in a dynamic fashion.

To give you some idea of what I am talking about.

In my code I have the arrayList dataCol that I want to populate with data. Statically I would do something like this if I wanted to add the data from the first name column.

dataCol.Add(customer.FirstName.GetValue().ToString());

But since I need to do this in a dynamic fashion, I have created a seperate ArrayList that holds all the column names of my database. To give an example, the columnName FirstName is inside the array.

Here is a bit more code. fldArr is the ArrayList that holds the names of the columns, qbFld is a string variable that holds the name of the column.

So my main question is, how do I take the data from fldArr and replace FirstName with the data?

Any help with this would be greatly appreciated.

for (int k = 0; k < fldArr.Count; k++)
{

if (customer != null)
{
string qbFld = fldArr[k].ToString();

dataCol.Add(customer.FirstName.GetValue().ToString());

}
}
 
I am working on an app where I require the use of dynamic members. In my app, I have the query customer that has several fields I want to be able to pull data from. Ideally I could do this statically, but I will need to use some of my previous classes to add more queries. So that is why I decided to do this in a dynamic fashion.
this doesn't make any sense. It sounds like you're using terms like static and dynamic out of context.

if you want to map your database columns to an entity in your system, then i would recommend using an ORM (object relational mapper) framework. Data access is a commodity and there are plenty of frameworks both OSS and $$$:
ActiveRecord, NHibernate, WilsonORMapper, SubSonic, LLBL Gen Pro

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Not sure how it doesn't make sense.

To sum it up

To statically assign a field name I would do something like this.

dataCol.Add(customer.FirstName.GetValue().ToString());

Now since I dont want to statically assign field names, I created an array that holds all the field names. So my question is this. How do I replace the member FirstName with a dynamic var.
 
c# is a compiled (not a dynamic) language, you can't just type
new object().MyProperty

you either need to create an object to map the properties to an array for you, or you need to use reflection and reflect the PropertyInfo from the associated type.

what dosen't make sense is why you need to create string arrays for setting database values. That's not OOP and it's not where the power of OOP lies.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top