Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to work with an object that was instantiated inside another object from a ASP.NET web form
    text
    copied!<p>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. </p> <p>For example, if I have a <code>customer</code> class that has a field called <code>contact</code> which is of type <code>Contact</code>, 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.</p> <p>This is the code from my <code>customer</code> class and <code>contact</code> is another class.</p> <pre><code>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></pre> <p>Code from my form</p> <pre><code>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; } </code></pre> <p>Is this the way I should be working with the <code>contact</code> object or should I have instantiated a <code>contact</code> object on the web form, populate it and then assign it to contact in the <code>customer</code> object or have I got it completely wrong and this should be done in a completely different way? </p> <p>Hope this makes sense.</p> <p>Thanks </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload