473,395 Members | 1,581 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,395 software developers and data experts.

Accessing child values by string name

I am trying to access the values of child properties by a string
value.

For instance, have a user type "Form1.txtFirstName.Text" in a text
box, click on a button, and return the value of that field.

Is this possible to do?

My first thought was to use reflection, but
I can only seem to tell what properties are contained in the class,
and not their values.

Another thought was to serialize the object to an XmlDocument and
find the values that way... but many of the objects I want to do
this with are not serializable. :(

Any help you can give me would be GREATLY appreciated!

Thank You.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

In order to reply via email, please remove the no_spam_for_me_ before
my real email address.
Dec 16 '05 #1
4 1391
You do use reflection. To actually get the value of the property you
can use InvokeMember with BindingFlags.GetProperty and other
BindingFlags set.

Dec 16 '05 #2
Hugh,

With reflection when you get, for example, PropertyInfo object for a
propoerty by its name you can call GetValue of this PopretyInfo object to
read the value of the property. The same goes for fields.
--

Stoitcho Goutsev (100) [C# MVP]

"Hugh O'Donnell" <no***********************@odonnell.tv> wrote in message
news:Xn**********************************@24.94.17 0.102...
I am trying to access the values of child properties by a string
value.

For instance, have a user type "Form1.txtFirstName.Text" in a text
box, click on a button, and return the value of that field.

Is this possible to do?

My first thought was to use reflection, but
I can only seem to tell what properties are contained in the class,
and not their values.

Another thought was to serialize the object to an XmlDocument and
find the values that way... but many of the objects I want to do
this with are not serializable. :(

Any help you can give me would be GREATLY appreciated!

Thank You.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

In order to reply via email, please remove the no_spam_for_me_ before
my real email address.

Dec 16 '05 #3
Doug & Stoitcho,

Thank you for your replies. You have sent me on a path to try the
code below... however, p is always null. Could you possibly show
me a working example?

Thanks!

public string GetValueFromText(object o, string s)
{
Type t = o.GetType();
PropertyInfo p = t.GetProperty(s); // p is always null.
return p.ToString();
}

executing with:
GetValueFromText(Form1, "txtResult");
You do use reflection. To actually get the value of the property you
can use InvokeMember with BindingFlags.GetProperty and other
BindingFlags set.


-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

In order to reply via email, please remove the no_spam_for_me_ before
my real email address.
Dec 16 '05 #4

"Hugh O'Donnell" wrote...
Thank you for your replies. You have sent me on a path
to try the code below... however, p is always null.
I believe you've confused "Property" with "variable name" here...

I don't believe you have a property "txtResult" in the object Form1, and if
the latter even is an object and not a class?
public string GetValueFromText(object o, string s)
{
Type t = o.GetType();
PropertyInfo p = t.GetProperty(s); // p is always null.
return p.ToString();
}

If the naming in your form is from the default of what e.g. Visual
Studio.NET produces, then it's more likely that "txtResult" is the variable
name of a TextBox or a Label, something that in turn have a property "Text".

You then need yet another level of reflection, e.g. like this:
public string GetValueFromText(object o1, string member)
{
Type t1 = o1.GetType();

// In case the field is private, you need to
// provide binding flags...
FieldInfo f1 =
t1.GetField(member,
BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

// Now get the field's type
object o2 = f1.GetValue(o1);
Type t2 = o2.GetType();

// ...and get the "Text" property from that...
PropertyInfo p2 = t2.GetProperty("Text");
return p2.GetValue(o2, null).ToString();
}
// Bjorn A
Dec 16 '05 #5

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

Similar topics

6
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is...
5
by: Ilan Sebba | last post by:
When it comes to adding records in related tables, Access is really smart. But when I try to do the same using ADO, I am really stupid. Say I have two parent tables (eg Course, Student) and one...
1
by: Moojjoo | last post by:
Good Monday Morning: I am at a dead stop and I am hoping some other developers out there can help me out. I have built a dataset that first contains a table populated from SQL Server then I am...
0
by: N. Demos | last post by:
Hello, I'm having problems accessing a complex XML child node (latitude & longitude), and passing it to a function when the XML file has been read into a DataSet. Specifically, the returned object...
1
by: adamredwards | last post by:
I have a page with some form elements that are dynamically generated. They are inserted into the dom by first cloning a node, changing the values like name, and then inserted with insertBefore(). ...
4
by: raj_genius | last post by:
I hav two queries, whc are as follows: FIRSTLY: is it possible to access the controls(by name) of a parent form(MDI) from its child forms??if yes then how??plzz provide a coded example in VB if...
4
by: SteveT | last post by:
I am wanting to populate several treeviews, one for the <TRs> group and one for the <TGsgroup. Is there a simplier way to populate the Treeview than the one I did below? It seems difficult to...
9
by: JohnR | last post by:
I have the name of a control in a string variable and I want to change one of the controls properties. Right now I recursively scan all the controls on the form until I get one whose name matches...
3
by: creative1 | last post by:
Here is how you create a complex data report that involves parent and child commands and you can update information at runtime. Its pretty straight forward to work with simple queries; however,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
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.