Remove item selected by user from list

I am trying to create a contact manager program in a console application using a list to store and display data. I need to view a report that displays a summary of available contacts, and then there is a menu that allows the user to interact with the program. I have a method for creating a contact and a contact object. I also have a way to delete a contact, but I want the user to be able to select the contact name and delete the deleted contact. However, I am not sure how to do this.

Any guidance would be appreciated.

static void Main(string[] args) { //Declare the list List<Contact> contactList = new List<Contact>(); //Main Driver char menuItem; Console.WriteLine("Contact List\n"); menuItem = GetMenuItem(); while (menuItem != 'Q') { ProcessMenuItem(menuItem, contactList); menuItem = GetMenuItem(); } Console.WriteLine("\nThank you, goodbye"); Console.ReadLine(); } //Returns either a 'C', 'R', 'U', 'D', 'L', or 'X' to the caller static char GetMenuItem() { char menuItem; DisplayMenu(); menuItem = char.ToUpper(IOConsole.GetChar("\nPlease pick an item: ")); while (menuItem != 'C' && menuItem != 'R' && menuItem != 'Q' && menuItem != 'U' && menuItem != 'D' && menuItem != 'S' && menuItem != 'L' && menuItem != 'F' && menuItem != 'P' && menuItem != 'T') { Console.WriteLine("\nError - Invalid menu item"); DisplayMenu(); menuItem = char.ToUpper(IOConsole.GetChar("\nEnter option or M for menu:")); } return menuItem; } static void DisplayMenu() { Console.WriteLine("C-> Create Contacts"); Console.WriteLine("R-> Remove Contacts"); Console.WriteLine("U-> Update Contacts"); Console.WriteLine("D -> Load data from file"); Console.WriteLine("S-> Save data to file"); Console.WriteLine("L-> View sorted by last name"); Console.WriteLine("F-> View sorted by first name"); Console.WriteLine("P-> View by partial name search"); Console.WriteLine("T-> View by contact type"); Console.WriteLine("Q-> Quit"); } //Routes to the appropriate process routine based on the user menu choice static void ProcessMenuItem(Char menuItem, List<Contact> contactList) { switch (menuItem) { case 'C': createContact(); break; case 'R': removeContact(contactList); break; case 'U': updateContact(contactList); break; case 'D': LoadFromFile(); break; case 'S': saveToFile(); break; case 'L': sortByLastName(contactList); break; case 'F': sortByFirstName(contactList); break; case 'P': DisplayList(contactList); break; case 'T': sortByContactType(); break; case 'Q': break; } } //allows the user to remove a contact public static void removeContact(List<Contact> contactList) { for (int i = 0; i < contactList.Count; i++) if (i % 5 == 0) contactList.RemoveAt(i); } 
+5
source share
3 answers

You can make your function as follows: it will take a List and contact name , and then remove the first occurrence of the contact name from the contact list.

  public static void removeContact(List<Contact> contactList, object ContactName) { Contact contactToRemove = (Contact)contactList.AsEnumerable().Where (x => x.ContactName == ContactName || x.ContactNumber == (int)ContactName).First(); contactList.Remove(contactToRemove); } 

my contact class:

  class Contact { private int _ContactNumber; public int ContactNumber { get { return _ContactNumber; } set { _ContactNumber = value; } } private string _ContactName; public string ContactName { get { return _ContactName; } set { _ContactName = value; } } } 

So your case would be something like this:

  case 'R': Console.WriteLine("enter the contact name/Contact Number To delete"); removeContact(contactList, Console.Read().ToString() ); break; 
+1
source

You can easily do this with a LINQ or Lambda Expression expression:

In the contact model class, enter a field that says the phone number itself.

When you select a contact, two options in

 removeContact(List<Contact> contactList,int selectedContactPhoneNo) 

You can simply do the following:

 contactList.RemoveAll(x => x.PhoneNo == selectedContactPhoneNo); 
+4
source

A simple approach would be to iterate over the contact list, and then use one of the properties of your contact as a search key.

 //Search contact via number //Depending which key you would want for example Contact Number: int number = 1; Contact contact = null; for (int i = 0; i < contactList.Count; i++) { if (contactList[i].Number == number) { contact = contactList[i]; //Assign contact break; } } //Remove contact from list if (contact != null) //If not null, it means we found it. Remove it from the list: { contactList.Remove(contact); } 

You can create a function for the above implementation to pass a specific keyword when searching for and removing an item from the list.

+1
source

All Articles