Solved

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

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

Turn Insights into Action

Communication across every corner of your business is essential to increase the velocity of your application delivery and support pipeline. Automate, standardize, and contextualize your communication processes with xMatters.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

689 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