字典不存储值

时间:2017-08-10 14:50:59

标签: c# dictionary

我的词典有问题" list"因为我在添加一个人后从菜单中调用方法removePeoplesaveToFile时,它不会打印或保存任何内容。当我在Main中评论循环并创建类Person的实例,然后手动调用所有方法时,它可以工作。我在这里缺少什么?

using System;
using System.Collections.Generic;
using System.IO;

namespace scope_and_accessibility
{
class Program
{
    static void Main(string[] args)
    {
        bool menu = true; 
        while (menu)
          {
             menu = Menu();
          } 
    }
    public static bool Menu()
    {
        Person person = new Person();
        int choice;
        Console.WriteLine("1. Add person");
        Console.WriteLine("2. Delete person");
        Console.WriteLine("3. Save data to text file");
        Console.WriteLine("4. Exit");
        choice = int.Parse(Console.ReadLine());
        switch(choice)
        {
            case 1:
                person.createPerson();
                return true;
            case 2:
                person.removePeople();
                return true;
            case 3:
                person.saveToFile();
                return true;
            case 4:
                return false;
            default:
                return true;
        }

    }
}
class Person
{
    private long choiceOfPerson = 0;
    private string completeInfo = "";
    public string firstName { get; set; }
    public string lastName { get; set; }
    public long personalIdentityNumber { get; set; }
    public string Gender { get; set; }
    DateTime dateOfBirth = new DateTime();
    Dictionary<long, string> list = new Dictionary<long, string>();

    public void createPerson()
    {
        Console.WriteLine("Enter personal identity number");
        personalIdentityNumber = long.Parse(Console.ReadLine());
        Console.WriteLine("Enter your first Name");
        firstName = Console.ReadLine();
        Console.WriteLine("Enter your last name");
        lastName = Console.ReadLine();
        Console.WriteLine("Enter your gender");
        Gender = Console.ReadLine();
        Console.WriteLine("Enter your date of birth: DD/MM/YYYY");
        bool error = true;
        {
            while (error)
                try
                {
                    dateOfBirth = DateTime.Parse(Console.ReadLine());
                    error = false;
                }
                catch (FormatException fEx)
                {
                    Console.WriteLine(fEx.Message);

                }
        }
        completeInfo = firstName + " " + lastName + " " + Gender + " " + dateOfBirth.ToString("dd/MM/yyyy");
        list.Add(personalIdentityNumber, completeInfo);
    }
    public void removePeople()
    {
        foreach (KeyValuePair<long, string> entry in list)
        {
            Console.WriteLine(completeInfo + " " + personalIdentityNumber);
        }
        Console.WriteLine("Write the perosnal identity number of person you wish to delete");
        choiceOfPerson = long.Parse(Console.ReadLine());
        list.Remove(choiceOfPerson);
    }
    public void saveToFile()
    {
        foreach (KeyValuePair<long, string> entry in list)
        {
            File.WriteAllText(@"E:\gry\info2.txt", completeInfo + personalIdentityNumber);
        }
    }
}

}

2 个答案:

答案 0 :(得分:2)

您的人员列表设置在您的Person类中。如果您创建Person的新实例,则另一个实例将被销毁。相反,您应该将列表引用保留在Person之外并将其放在Program类中。

您应该只从Person类外部操作列表。 Person类只是表示Person的对象。它不能包含自己的列表。

class Program
{
    private static Dictionary<long, string> list = new Dictionary<long, string>();

    static void Main(string[] args)
    {
        bool menu = true; 
        while (menu)
          {
             menu = Menu();
          } 
    }
    public static bool Menu()
    {
        Person person = new Person();
        int choice;
        Console.WriteLine("1. Add person");
        Console.WriteLine("2. Delete person");
        Console.WriteLine("3. Save data to text file");
        Console.WriteLine("4. Exit");
        choice = int.Parse(Console.ReadLine());
        switch(choice)
        {
            case 1:
                person.createPerson();
                var personInfoKey = string.Format("{0} {1} {2} {3}", person.firstName, person.lastName, person.Gender, person.dateOfBirth); 
                list.Add(person, personInfoKey );
                return true;
            case 2:
                person.removePeople();
                list.Remove(person);
                return true;
            case 3:
                person.saveToFile();
                return true;
            case 4:
                return false;
            default:
                return true;
        }

    }
}
class Person
{
    private long choiceOfPerson = 0;
    private string completeInfo = "";
    public string firstName { get; set; }
    public string lastName { get; set; }
    public long personalIdentityNumber { get; set; }
    public string Gender { get; set; }
    DateTime dateOfBirth = new DateTime();

    public void createPerson()
    {
        Console.WriteLine("Enter personal identity number");
        personalIdentityNumber = long.Parse(Console.ReadLine());
        Console.WriteLine("Enter your first Name");
        firstName = Console.ReadLine();
        Console.WriteLine("Enter your last name");
        lastName = Console.ReadLine();
        Console.WriteLine("Enter your gender");
        Gender = Console.ReadLine();
        Console.WriteLine("Enter your date of birth: DD/MM/YYYY");
        bool error = true;
        {
            while (error)
                try
                {
                    dateOfBirth = DateTime.Parse(Console.ReadLine());
                    error = false;
                }
                catch (FormatException fEx)
                {
                    Console.WriteLine(fEx.Message);

                }
        }
        completeInfo = firstName + " " + lastName + " " + Gender + " " + dateOfBirth.ToString("dd/MM/yyyy");
        list.Add(personalIdentityNumber, completeInfo);
    }
    public void removePeople()
    {
        foreach (KeyValuePair<long, string> entry in list)
        {
            Console.WriteLine(completeInfo + " " + personalIdentityNumber);
        }
        Console.WriteLine("Write the perosnal identity number of person you wish to delete");
        choiceOfPerson = long.Parse(Console.ReadLine());
        list.Remove(choiceOfPerson);
    }
    public void saveToFile()
    {
        foreach (KeyValuePair<long, string> entry in list)
        {
            File.WriteAllText(@"E:\gry\info2.txt", completeInfo + personalIdentityNumber);
        }
    }
}

答案 1 :(得分:-2)

using System.Linq;

var text = string.Join(Environment.NewLine), list.Select(entry => $"
                         {completeInfo} {personalIdentityNumber}"));
File.WriteAllText(path, text);