Passing data between forms
If I were to write a program, what methods would I use to send information from the main form of the application to a modal popup form and then pass any changes to the data back to the main form when the popup form was closed? This was an interview question that I faced and was not sure how to answer. What would your reply be?

    Requires Free Membership to View

    When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to provide a unique online resource for developers, architects and development managers tasked with building and maintaining enterprise applications using Visual Basic, C# and the Microsoft .NET platform.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchWinDevelopment.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchWinDevelopment.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

I would create classes or structures to represent the data being passed, and then create a constructor in the Form class for the Form to be shown that would allow me to pass the information to the class at creation time. I would also expose the information from the class using a property to allow me to retrieve the results after the Form closed. This is a very simple example to help you focus on the ideas at play. How to pass information between various classes and retrieve the information later.

Regards,
Mark

public class Startup
{ 
 [STAThread()]
 public static void Main(string[] args)
 {
  // create some contact info to be edited
  ContactInformation info = new ContactInformation(@"Mark Belles", @"555 C# Drive"); 
  
  // create a form to display and allow the info to be edited
  ContactInformationForm form = new ContactInformationForm(info);
  
  // show the form modally
  if (form.ShowDialog() == DialogResult.OK)
  {
   // retrieve the updated info, and do something with it, perhaps save it somewhere...
   info = form.ContactInformation;
   
   // display the contact information...
   Debug.WriteLine(info);
  }  
 }
}

public class ContactInformation
{
 protected string _name;
 protected string _address;
 
 public ContactInformation(string name, string address)
 {
  _name = name;
  _address = address;
 }
 
 public string Name
 {
  get
  {
   return _name;
  }
  set
  {
   _name = value;
  }
 }
 
 public string Address
 {
  get
  { 
   return _address;
  }
  set
  {
   _address = value;
  }
 }
 
 public override string ToString()
 {
  return string.Format("ContactInformation: '{0}', '{1}'", _name, _address);
 }
}

public class ContactInformationForm : Form
{
 protected ContactInformation _info;
  
 public ContactInformationForm(ContactInformation info)
 {
  _info = info;
  // display the information for editing
 }
 
 public ContactInformation ContactInformation
 {
  get
  {
   return _info;
  }
 }
}

This was first published in February 2005