/// <summary> /// Tries to add a contact in the registry /// </summary> /// <param name="contactIn"></param> /// <returns>True/false</returns> public bool AddContact(Contact contactIn) { // Check validity to avoid program termination if (contactIn != null) { // Use copy constructor contactRegistry.Add(new Contact(contactIn)); return true; } return false; }
/// <summary> /// Copy Constructor /// </summary> /// <param name="origContact"></param> public Contact(Contact origContact) { this.firstName = origContact.firstName; this.lastName = origContact.lastName; this.address = new Address(origContact.address); }
/// <summary> /// Reads names and address information and saves it /// </summary> /// <returns>True or false</returns> private bool ReadInput() { // User input string firstName, lastName; Address address; // MessageBox info string message = String.Empty; string caption = "Input Error"; if (!(ReadName(out firstName, out lastName))) { // Assemble messagebox info depending on which error message = "Name must contain at least one character, and may not consist of only empty spaces"; MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } if (!(ReadAddress(out address))) { message = "Address fields must contain at least one character, and may not consist of only empty spaces"; MessageBox.Show(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } // Save info this.contact = new Contact(firstName, lastName, address); return true; }
/// <summary> /// Tries to change a selected contact, by removing the old record and adding a new /// </summary> /// <param name="contactIn"></param> /// <param name="index"></param> /// <returns>True/false</returns> public bool ChangeContact(Contact contactIn, int index) { // Check validity to avoid program termination if (contactIn != null) { // Use copy constructor contactRegistry.Insert(index, new Contact(contactIn)); // Delete old record contactRegistry.RemoveAt(index+1); return true; } return false; }
/// <summary> /// Creates a deep copy instead of a reference /// </summary> /// <param name="index"></param> /// <returns>A copy of a Contact object</returns> public Contact GetContactCopy(int index) { // Fetch reference to item at selected index Contact origObj = this.contactRegistry[index]; // Use copy constructor to allocate a new object on the heap Contact copyObj = new Contact(origObj); return copyObj; }