Solved

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

Posted on 2013-01-02
2
248 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 500 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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