如何使用switch中另一个类的方法?

时间:2013-06-12 07:39:55

标签: c# .net methods switch-statement

我有点问题。如何在类Menu中调用类Person的方法到Switch-case。 不管我做什么。 感谢您的帮助。

CLASS MENU


static class Menu 
{

   public static void MainText()
    {
        Console.WriteLine("1. Add new person");
        Console.WriteLine("2. Print all");
        Console.WriteLine("3. Find item by name");
        Console.WriteLine("4. Delete item by ID");
        Console.WriteLine("5. Exit");
        Console.WriteLine("\n");
    }

    public static void MainStart()
   {
       string choice = "";

       do
       {
           MainText();
           Console.WriteLine("Enter your choice: ");
           choice = Console.ReadLine();


           switch (choice)
           {
               case "1": 

                   break;


           }
       } while (choice != "5");
   }
}

CLASS PERSON


public class Person
{
    private int ID = 0;
    private string FirstName;
    private string LastName;
    private string PhoneNumber;
    Person p = new Person();
    List<Person> H = new List<Person>();

    public Person(string fName, string lName, string phoneNum)
    {
        FirstName = fName;
        LastName = lName;
        PhoneNumber = phoneNum;
    }
    public Person()
    {
    }

    public void PrintAll()
    {
        foreach (Person item in H)
        {
            Console.WriteLine("First name: {0}", FirstName);
            Console.WriteLine("Last name: {0}", LastName);
            Console.WriteLine("Phone number: {0}", PhoneNumber);
            Console.WriteLine("ID: {0}", ID);
        }
    }

    public override string ToString()
    {
        return "First name: " + FirstName + "\n" + "Last name: " + LastName + "\n" + "Phone number: " + PhoneNumber + "\n" + "ID: " + ID + "\n";
    }

    public void AddPerson()
    {
        string fn = "";

        do
        {
            Console.WriteLine("Enter first name: ");
            fn = Console.ReadLine();
            if (fn == "")
            {
                Console.WriteLine("First name can't be blank");
            }
        }
        while (fn == "");
        p.FirstName = fn;
        Console.WriteLine("Enter last name: ");
        p.LastName = Console.ReadLine();
        Console.WriteLine("Enter phone number: ");
        p.PhoneNumber = Console.ReadLine();
        p.ID++;

        H.Add(p);
    }

    public void FindPerson()
    {
        Console.WriteLine("Please enter first or last name: ");
        string search = Console.ReadLine();

        foreach (Person item in H)
        {
            if (search == item.FirstName || search == item.LastName)
            {
                p.ToString();
                Console.WriteLine("*****************");
            }
        }
    }

    public void DeletePerson()
    {
        H.ToString();
        Console.WriteLine("Enter an ID to delete: ");
        int id = int.Parse(Console.ReadLine());

        H.RemoveAt(id);

        Console.WriteLine("Person deleted successfully");

    }
}

3 个答案:

答案 0 :(得分:1)

    Person pr = null;

    switch (choice)
       {
           case "1": 
               pr = new Person("FNAME", "LNAME", "012345678");
               pr.AddPerson();
               break;


       }

答案 1 :(得分:-1)

如果要将它们称为“Person.AddPerson”,则需要声明方法'static',而不是在已经创建并分配给某个变量的类的实例上调用方法(例如'thisPerson')。

答案 2 :(得分:-1)

您已将类Menu声明为Static和Class Person作为普通非静态类。无法在静态类中调用非静态类成员。如果你想要这样做,你需要使Person类中的方法也是静态的。没有其他方法可以在静态类中调用非静态方法。