473,763 Members | 9,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2140
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(fieldIn fo.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.co m

"Ric#k" <Ri***@discussi ons.microsoft.c om> wrote in message
news:5E******** *************** ***********@mic rosoft.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***@discussi ons.microsoft.c om> 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.GetPropert ies, or get
the property for a particular name using Type.GetPropert y. You can then
set or get their values. Which part are you having trouble with?

--
Jon Skeet - <sk***@pobox.co m>
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
9634
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 populate with a reference to the object that constructed this current object. i would attempt to accomplish this by setting the appropriate field from within the constructor... i figured it might be obtainable from the stack trace, but that...
12
2109
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
2101
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 class from within a Shape class. I want the user of the Shape class to be able to see the location of the Shape but not be able to change it. So I declare a read-only property: class Shape { private Point location; /* other Shape stuff */
4
1662
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 System.Web.HttpApplication but it does not seem to like that one bit. I also have a MSXML2.DOMDocument30 defined as a public object within the vb module which is loaded during the application start up with a bunch of XML. What is weird is that the data contained...
3
2105
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 when I pass a class as a parameter to a Windows Form. Basically, I have a class that has several fields, two of these fields are an instance of an inner class, the rest are basic value types (bool's in this case). I have a windows form, the...
7
10497
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 called wheels. I have declared the following: int number_of_wheels( const car &new_wheels)
5
2483
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 need to add properties to a function object and access them within the function when it is executing. Therefore, I need to be
1
8370
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 instance of an object.'" I've traced the problem to a custom control I created that inherits from Inherits System.Windows.Forms.TextBox. In this custom control, I have two constructors. I'm not sure why I created the second constructor -- it was so...
3
3145
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 object. I know I get this error because Themes is null ... I just don't know how to solve it without needing to do:
0
9564
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
10148
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
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9823
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...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5270
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...
1
3917
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
3
2794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.