Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 462145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:02:43+00:00 2026-05-12T23:02:43+00:00

I’ve searched throughout but can’t find the exact answer to my question. Take for

  • 0

I’ve searched throughout but can’t find the exact answer to my question. Take for instance the following code:

public class Company
{
    private string m_strName;
    private Customer m_objCustomer;

    public Company()
    {
        m_strName = "";
        m_objCustomer = new Customer();
    }

    public string Name
    {
        get { return m_strName; }
        set { m_strName = value; }
    }

    public Customer CustomerInformaion
    {
        get { return m_objCustomer; }
        set { m_objCustomer = value; }
    }
}

public class Customer
{
    private string m_strName;
    private Details m_objDetails;


    public Customer()
    {
        m_strName = "";
        m_objDetails = new Details();
    }

    public string Name
    {
        get { return m_strName; }
        set { m_strName = value; }
    }

    public Details CustomerDetails
    {
        get { return m_objDetails; }
        set { m_objDetails = value; }
    }
}

public class Details
{
    private string m_strPhoneNumber;
    private string m_strEmailAddress;

    public Details()
    {
        m_strPhoneNumber = "";
        m_strEmailAddress = "";
    }

    public string PhoneNumber
    {
        get { return m_strPhoneNumber; }
        set { m_strPhoneNumber = value; }
    }

    public string EmailAddress
    {
        get { return m_strEmailAddress; }
        set { m_strEmailAddress = value; }
    }
}

Now, I have setup a Form that has many text fields where a user can enter information about a customer at a company. One of those fields is the Email Address text field that has a Tag property set to EmailAddress. I want to be able to look at the Tag of the TextBox and iterate through the entire Company object to find a property with a matching Name and Set its value to the Text property of the TextBox. I can locate the property but setting its value has turned out to be quite difficult. Here’s what I have thus far:

foreach (PropertyInfo info in m_objCompany.GetType().GetProperties())
{
    if (info.PropertyType != typeof(System.String))
    {
        foreach (PropertyInfo info2 in info.PropertyType.GetProperties())
        {
            if (objTextBox.Tag.Equals(info2.Name))
            {
                if (info2.CanWrite)
                {
                    Object objValue = Convert.ChangeType(objTextBox.Text, info.PropertyType);
                    info2.SetValue(m_objCompany, objValue, null);
                }
            }

        }
    }
}

My issue is that when I run the code I receive an error at ChangeType and/or SetValue. The issue is that the Reflection is stopping at info2 and attempting to set the value to the Type of Details – since it is the parent of the Property EmailAddress.

Any help in determining how to point the SetValue to appropriate property would be helpful and appreciated. As I’m sure you well can guess my class is a GREAT deal larger than the example provided with near 100 properties. Most all are string values which will be entered in manually through TextBox objects. I’m attempting to create one routine that can then be called by all TextBox objects whereby the Tag property of the object could be used to indicate which Property of my class I’m trying to set. From there it is off to XML serialization land.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:02:43+00:00Added an answer on May 12, 2026 at 11:02 pm

    your innermost line

    info2.SetValue(m_objCompany, objValue, null);
    

    is trying to set value of the inner property (info2), on the outer object. The outer object doesn’t have an inner object.

    What you probably want, is something like this:

        public void Bar(object m_objCompany)
        {
            foreach (PropertyInfo info in m_objCompany.GetType().GetProperties())
            {
                if (info.PropertyType != typeof(System.String))
                {
                    // Somehow create the outer property
                    object outerPropertyValue = info.PropertyType.GetConstructor(new Type[] { }).Invoke(new object[] { });
    
                    foreach (PropertyInfo info2 in info.PropertyType.GetProperties())
                    {
                        if ("blah" == "blah")
                        {
                            if (info2.CanWrite)
                            {
                                Object innerPropertyValue = Convert.ChangeType("blah", info2.PropertyType);
                                info2.SetValue(outerPropertyValue, innerPropertyValue, null);
                            }
                        }
    
                    }
    
                    info.SetValue(m_objCompany, outerPropertyValue, null);
                }
            }
        }
    

    When you come across a property that you want to set, you need to create that property (outerPropertyValue), then setup the properties of that property (via innerPropertyValue), then set the outer property on the original object (m_objCompany).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
Seemingly simple, but I cannot find anything relevant on the web. What is the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.