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 9090627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:25:03+00:00 2026-06-16T22:25:03+00:00

This is probably a very simple question but I’m a little confused when it

  • 0

This is probably a very simple question but I’m a little confused when it comes to working with an object created from a class that contains a field where the type is another class. The part that confuses me is how to populate the field with data from the web form.

For example, if I have a customer class that has a field called contact which is of type Contact, what is the proper way to populate this at the web form level? At present I am doing it as follows and would like to know if this is correct.

This is the code from my customer class and contact is another class.

public class Customer
{
    private string firstName;
    private string lastName'
    private string notes;
    private Contact contact;

    public Customer()
    {
        this.contact = new Contact();
    }

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    public string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }
    public string Notes
    {
        get { return notes; }
        set { notes = value; }
    }
    public Contact Contact
    {
        get { return contact; }
        set { contact = value; }
    }
}

Code from my form

private Customer customer;

public WebFormSample()
{
     customer = new AddressBook.Customer();
}
protected void addButton_Click(object sender, EventArgs e)
{
     customer.FirstName = firstName.Text;
     customer.LastName = lastName.Text;
     customer.Contact.Phone = phone.Text;
     customer.Contact.Email = Email.Text;
}

Is this the way I should be working with the contact object or should I have instantiated a contact object on the web form, populate it and then assign it to contact in the customer object or have I got it completely wrong and this should be done in a completely different way?

Hope this makes sense.

Thanks

  • 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-06-16T22:25:05+00:00Added an answer on June 16, 2026 at 10:25 pm

    Did you try the code you posted? Because I would expect customer.Contact to be null as you never created an instance for it.

    So you can fix your existing code like this:

    customer.Contact = new Contact();
    customer.Contact.Phone = phone.Text;
    customer.Contact.Email = Email.Text;
    

    Or you can do as you were asking about:

    var contact = new Contact();
    contact.Phone = phone.Text;
    contact.Email = Email.Text;
    customer.Contact = contact;
    

    Depending on your needs, either should work.

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

Sidebar

Related Questions

This is probably a very simple question but I'm a little confused how this
I know this is probably a very simple question but how would I do
This is another probably very simple question, but I haven't been able to find
This is probably a very simple question so please forgive my ignorance, but can
I'm new to Xcode. This is probably a very simple question to you, but
This is probably a very simple Object Oriented Programming question. What I am trying
This is probably a very simple question, but I can't find the answer, so
Okay, this is (probably) a very simple question, but I am afraid I know
This is probably a very simple question that I am working through in an
I feel like the answer to this question is probably very simple, but I'm

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.