473,407 Members | 2,629 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,407 software developers and data experts.

reference all properties of a class within the constructor

Is it possible to reference all the public properties that are in a class
within that class's constructor using REFLECTION, and then reference them
with a string variable?
I have a class that will have 35 integer values as public properties that
must be read in from a database. Also, each property of this class will have
the same name as the column name from the dbase table. For that reason, I
would like to just set up a loop to get the property name from the GetName
property in the DataReader, and then assign the value from the read field.
Otherwise I will have to assign each property from the table individually.
Nov 16 '05 #1
2 2121
Ric,

Absolutely, here is how you would do it:

// Get all the fields in the class, and map them to the field name.
FieldInfo[] fields = this.GetType().GetFields();

// Cycle through the fields, and add them to a hashtable.
Hashtable map = new Hashttable();

// Cycle.
foreach (FieldInfo fieldInfo in fields)
{
// Add to the hashtable.
map.Add(fieldInfo.Name, fieldInfo);
}

// Make the database call. As you cycle through the fields, you would get
the field info, and set the value.
((FieldInfo) map["column"]).SetValue(this, value);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ric#k" <Ri***@discussions.microsoft.com> wrote in message
news:5E**********************************@microsof t.com...
Is it possible to reference all the public properties that are in a class
within that class's constructor using REFLECTION, and then reference them
with a string variable?
I have a class that will have 35 integer values as public properties that
must be read in from a database. Also, each property of this class will
have
the same name as the column name from the dbase table. For that reason, I
would like to just set up a loop to get the property name from the GetName
property in the DataReader, and then assign the value from the read field.
Otherwise I will have to assign each property from the table individually.

Nov 16 '05 #2
Ric#k <Ri***@discussions.microsoft.com> wrote:
Is it possible to reference all the public properties that are in a class
within that class's constructor using REFLECTION, and then reference them
with a string variable?
I have a class that will have 35 integer values as public properties that
must be read in from a database. Also, each property of this class will have
the same name as the column name from the dbase table. For that reason, I
would like to just set up a loop to get the property name from the GetName
property in the DataReader, and then assign the value from the read field.
Otherwise I will have to assign each property from the table individually.


Well, you can get all the properties using Type.GetProperties, or get
the property for a particular name using Type.GetProperty. You can then
set or get their values. Which part are you having trouble with?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Murat Tasan | last post by:
i have a quick question... is there a way to obtain the reference to the object which called the currently executing method? here is the scenario, i have a class and a field which i would like to...
12
by: Simon Jefferies | last post by:
Hello, I have created a new form within C# and I am unable to get any properties on this form and also unable to add a new controls to it. Any ideas? Simon Jefferies
2
by: Cory Burkhardt | last post by:
I am not sure what the suggested practice is for returning reference types from read-only properties. Consider an example: Suppose I have a Point class that is a reference type. I use the Point...
4
by: Keith Chadwick | last post by:
I am having some trouble referencing an Application("myVar") variable from within a module.vb file on my ASP.NET site. According to the documentation I should be able to reference...
3
by: Ross McLean | last post by:
Hi all, I've been teaching myself C# for a new project at work. I have a bit of a background in c++ and java but never been what you could call a guru. I'm having some strange things happening...
7
by: andy | last post by:
A question about about passing a class by reference: Say you have a class called car, and within that you have two objects called car01 and car02. Within the class I have an int variable...
5
by: reycri | last post by:
Hi, I need to be able to do this: var func = new Function("var me = <selfRef>; alert(me.params);"); func.params = "This is a test parameter"; window.setTimeout(func, 500); Basically, I...
1
by: Don | last post by:
I'm getting the following exception displayed in the task list at design time for my project: "Code generation for property 'Controls' failed. Error was: 'Object reference not set to an...
3
by: shapper | last post by:
Hello, I have the following loop and error message: foreach (Theme theme in Profile.Collaborator.Themes) { ... And I get the following error: Object reference not set to an instance of an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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
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,...

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.