Solved

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now