473,225 Members | 1,375 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,225 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 1381
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,...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.