How to work with objects that are instantiated inside another object from a web form
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 eample, 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; } } }
Is this the way I should be working with the contact object or should I 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.
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
Your solution does raise one question though, what if there is a need to instantiate the object when loading the form? Say you want to populate the various fields of Customer or Contact using the change event of a control on the form instead of doing everything in the add_button method, how would this scenario be accomplished?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Your solution does raise one question though, what if there is a need to instantiate the object when loading the form? Say you want to populate the various fields of Customer or Contact using the change event of a control on the form instead of doing everything in the add_button method, how would this scenario be accomplished?