Solved

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

Posted on 2013-01-02
2
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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