Dmitrii Trubetskoy
Dmitrii Trubetskoy

Reputation: 23

How can I modify a list which is already created?

I have a clients list that I need to access (from a .txt file) and to change any of its lines and (for ex Change telephone number)

I have already a code that opens a .txt file, reads from it, allows me to add new information and save it to the same file afterwards. The list, therefore, becomes updated. As well it allows me to search by keyword inside the list.

   public static void ShowClientList() {

        string file =File.ReadAllText(@"C:\Users\Adminl\Documents\clients.txt");
        Console.WriteLine(file);
        Console.WriteLine();
    }

 public static void AddClient()
    {   Console.WriteLine("Civ");
        string civility = Console.ReadLine();                        
        Console.WriteLine("Name");
        string name = Console.ReadLine();                        
        Console.WriteLine("Surname");
        string surname = Console.ReadLine();                        
        Console.WriteLine("Age");
        string age = Console.ReadLine();                        
        Console.WriteLine("Telephone No");
        string telephone = Console.ReadLine();                       

        string appendText = civility +','+" "+ name + ',' + " " + surname + ',' + " " + age + ',' + " " + telephone + Environment.NewLine;        // This text is always added, making the file longer over time if its not deleted

        string path = @"C:\Users\Adminl\Documents\clients.txt";     // FILE that either exist or no


        File.AppendAllText(path, appendText);
    }

  public static void SearchClients()
    {

        string line;
        StreamReader clients = new StreamReader(@"C:\Users\Adminl\Documents\clients.txt");  // Read the file and display it line by line.  

            List<string> lines = new List<string>();


            while ((line = clients.ReadLine()) != null)
            {
                lines.Add(line);
            }

            Console.WriteLine("Please insert the criteria: ");
            string choose = Console.ReadLine();

            for (int i = 0; i < lines.Count; i++)
            {

                if (lines[i].ToUpper().Contains(choose.ToUpper()))           // search any part
                {
                    Console.WriteLine(lines[i]);
                }
            }

     }

I'd like to choose the line from the list (of clients) and be able to change it. If possible, also give a unique number to every line. Sorry I am a total newbie, so please don't hit me too much. Thanks a lot for the help!

Upvotes: 0

Views: 88

Answers (2)

user4318630
user4318630

Reputation:

For your another question "If possible, also give a unique number to every line."

Answer is that you may not give unique number to a line using List, instead you may try Dictionary as it has unique key which a list does not. Please find the declaration of using List and Dictionary

List

List<string> lines = new List<string>();

Dictionary

Dictionary<int, string> lines = new Dictionary<int, string>();

You can get more details about Dictionary in the link

Upvotes: 0

user4318630
user4318630

Reputation:

You could get the particular index by matching the values using List.FindIndex method. In your code, you may find by matching the choose value you declared as below.

 string choose = Console.ReadLine();
 int index= lines.FindIndex(value => value== choose);

Also, you may change the line by directly passing the value through the index as below

 lines[index] = "ModifiedLine";

Upvotes: 1

Related Questions