Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I keep on going crazy, programming c # but are stuck on a sentence which I did not solve, I'm pretty new to C #. My question is what does the following:

- The CustomerForm object should not directly do any action on the customer registry
(customerManager) of the MainForm.. In other words, the MainForm should not pass the whole
registry object (customerManager) to the CustomerForm object. The MainForm should not pass a
reference to itself either. The communication between the MainForm can take place at least in two
ways:

- Declare a field in the CustomerForm that is of the type Customer. Write then a set and get
method. The customer object can transport information for a customer that is added, or is to be changed using the get and set
properties

- A customer object (and other information) can be passed by reference to the CustomerForm as argument in the CustomerForm’s
constructor

How should I encode this? Please help me
Posted
Updated 1-Nov-11 6:33am
v2

Method 1:
C#
Public Customer Customer { get; set; }
You can expand this with a backing store and do any manipulations you need to upon that.

Method 2:
C#
public CustomerForm(ref Customer customer)
   {
   ...
   }
 
Share this answer
 
Homework assignment?

1. Sounds like you have two forms; CustomerForm and MainForm.

2. The MainForm has a customerManager object and you are not permitted to pass it to the CustomerForm.

3. I'm guessing your instructor wants you to encapulate the customerManager in the Customer object and then pass the Customer object into the CustomerForm.

possible example:

C#
class Customer
{
   private CustomerManager myCustomerMgr
   public Customer(CustomerManager mgr)
   {
     myCustomerMgr = mgr;
   }

   //write your get and sets

}
inside MainForm:

C#
Customer c = new Customer(_mainFormsCustomerManager);

CustomerForm frm = new CustomerForm(c);

Does that help?
 
Share this answer
 
The second approach sounds like a general purpose modal editor pattern I use quite a bit. Typically I pass the object to edit into (a new overload of) ShowDialog and then modify it if the dialog was closed successfully, i.e.

public DialogResult ShowDialog(IWin32Window owner, Customer c){
 this.name.Text = c.Name;
 this.address.Text = c.Address;
 // ... etc; copy c's properties into UI controls
 
 DialogResult r = ShowDialog(owner);
 if(DialogResult.OK == r){
  c.Name = this.name.Text;
  c.Address = this.address.Text;
  // ... etc; copy properties back into c
 }
 return r;
}


Note that this pattern is only appropriate for an editor that doesn't save any changes until you close it (or with some modifications, with a Save button or similar). Don't use it if you want live updates, like a property editor panel – use data binding for that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900