0

If I want to register a user in class “add user” and then list all registered users in another class “list users” how should code be?

I have Person class

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Person(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
}

Then I Have window for Adding/registry

class AddView
{

    Dictionary<string, Person> _persons = new Dictionary<string, Person>();

    public void Display()
    {

        Console.WriteLine("Firstname");
        var firstname = Console.ReadLine();

        Console.WriteLine("Lastname");
        var lastname = Console.ReadLine();

        var per = new Person(firstname,lastname);

        _persons.Add(per.FirstName, per);

        Console.WriteLine("added");
        Console.ReadKey();

        var main = new MainView();
        main.Display();
    }
}

Then I want to display all I added in different window/ class

class ListView
{

    public void Display()
    {

Here comes this problem I cant get access to data that I registered In Add Person class

        foreach (var p in _persons)
        {
            Console.WriteLine(p.Value.FirstName);

        }

    }
}
dawid
  • 1
  • 2
  • 1
    I think you need to explain what you have so far and what you are trying to achieve. Classes based on verb phrases like "add user" and "list user" should generally be avoided so it sounds like you are heading down the wrong path, but without seeing more of what you are trying to do it is difficult to guide you. I may expect to see one class that has an "Add user" and "List users" functions or methods tho', so perhaps your terminology is incorrect rather than the code structure. – Colin Mackay Dec 24 '18 at 12:03
  • Ok again I edited post – dawid Dec 24 '18 at 12:40
  • Anyone know how to solve this problem? – dawid Dec 25 '18 at 12:42

0 Answers0