?
Solved

How to work with objects that are instantiated inside another object from a web form

Posted on 2013-01-02
2
Medium Priority
?
253 Views
Last Modified: 2013-03-07
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; }
        }
    }

Open in new window


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;
}

Open in new window


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.

Thanks
0
Comment
Question by:obb-taurus
2 Comments
 
LVL 25

Accepted Solution

by:
jogos earned 2000 total points
ID: 38739034
Both  versions off passing the object or passing the values and creating the object in the constructor.
public class Customer
    {
 .....
        // when creating Contact-object in your constructor
        public Customer(string aFirstName, string aLastName
                  , string aPhone, string aEmail)
        {
            this.FirstName  = aFirstName;
            this.LastName = aLastName;
            this.Contact = new Contact(aPhone,aEmail);
        }
       // when passing Contact-object 
        public Customer(string aFirstName, string aLastName
                  , Contact aContact)
        {
            this.FirstName  = aFirstName;
            this.LastName = aLastName;
            this.Contact = aContact;
        }
...

Open in new window


Create your Customer only when you realy create one, so in the addButton.
public WebFormSample()
{
}

protected void addButton_Click(object sender, EventArgs e)
{
          // Version 1
         Customer customer = new AddressBook.Customer(firstName.Text,
                   lastName.Text, phone.Text, Email.Text);

         // Or Version 2
         Contact c = new Contact(phone.Text, Email.Text);
         Customer customer2 = new AddressBook.Customer(firstName.Text,
                   lastName.Text, c);
}

Open in new window

0
 

Author Comment

by:obb-taurus
ID: 38739115
Thanks.

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?
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question