Home > Blockchain >  How can I extract data from "List <Contact>" of object and paste it into file.txt in
How can I extract data from "List <Contact>" of object and paste it into file.txt in

Time:05-15

static List<Contact> ListOfContact = new List<Contact>(); //Sorce of data: 
    
public void SaveContactToFile() //method to save a data
{
    try
    {
        FileName = @"contacts.txt";
        // Check if file already exists. If no, create it.     
        if (!File.Exists(FileName))
        {
            File.Create(FileName);

            using (TextWriter tw = new StreamWriter(FileName))
            {
                foreach (String s in ListOfContact.verbList)
                    tw.WriteLine(s);
            }
        }      
    }
    catch (Exception ex)
    { Console.WriteLine(ex.Message); }

CodePudding user response:

Your ListOfContact is a list of Contact instances. The list itself has no verbList member. Therefore, the code in your question will not even compile.

If you want to access the verbList member of each Contact instance, iterate over the list and access the verbList member for each Contact instance:

foreach (var contactInstance in ListOfContact)
{
    ... do stuff with contactInstance.verbList ...
}

CodePudding user response:

There's so much missing in your code. Firstly ListOfContact is a list but you're trying to access verbList from it, which doesn't make sense.

Here's an example you can use...

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            List<Contact> Contacts = new List<Contact>{
                new Contact("John", "000-000-0000"),
                new Contact("Peter", "000-000-0001"),
                new Contact("George", "000-000-0001")
            };

            var fileName = "contacts.txt";

            //FileStream file = File.Create(fileName);

            Console.WriteLine("Writting to File");
            Console.WriteLine(fileName);

            using(StreamWriter writer = new StreamWriter(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName)))
            {
                foreach(var contact in Contacts)
                {
                    Console.WriteLine(contact.Print());
                    writer.WriteLine(contact.Print());
                }
            }
            Console.WriteLine("Complete...");
            Console.WriteLine("Press any key to continue");
            Console.ReadLine();
        }

        public class Contact
        {
            public string Name { get; set; }
            public string Number { get; set; }

            public Contact(string name, string number)
            {
                Name = name;
                Number = number;
            }

            public string Print()
            {
                return $"{Name} - {Number}";
            }
        }
    }
}

CodePudding user response:

Welcome to Stack Overflow.Please try to be more descriptive and detailed in your questions like the rest have pointed out.If your main question was why the SaveContactToFile method is not writing to file for your object ListOfContact. the Textwriter method WriteLine writes to the textstream.To have the actual buffered data written to the underlying stream you will need to call tw.Flush();

and close the stream just after exiting the loop as good practice tw.Close();

  •  Tags:  
  • c#
  • Related