类型' System.FormatException'的未处理异常发生了

时间:2015-07-30 05:24:50

标签: c#

我正在尝试使用列表来存储和显示数据,从而在控制台应用程序中构建联系人管理器程序。我需要查看显示可用联系人摘要的报告,然后有一个菜单允许用户与该程序进行交互。我有一个方法来创建一个联系人和一个联系人对象。我还有一种更新联系人的方法,但我想让用户能够选择联系人的名字,姓氏,电子邮件地址,电话号码,类型,并能够使用用户输入的信息进行更新。但是在这一行Console.WriteLine(" \ nUpdate联系人:{1}",contactList);我一直收到错误:

  

未处理的类型' System.FormatException'发生了,   附加信息:索引(从零开始)必须大于或   等于零且小于参数列表的大小。

任何指导都将不胜感激。

static void Main(string[] args)
    {         
        //Declare the list

        List<Contact> contactList = new List<Contact>();

        //Main Driver
        char menuItem;
         Console.WriteLine("Contact List\n");
        menuItem = GetMenuItem();
        while (menuItem != 'Q')
        {

            ProcessMenuItem(menuItem, contactList);
            menuItem = GetMenuItem();

        }
        Console.WriteLine("\nThank you, goodbye");
        Console.ReadLine();
    }
    //Returns either a 'C', 'R', 'U', 'D', 'L', or 'X' to the caller
    static char GetMenuItem()
    {
        char menuItem;
        DisplayMenu();
        menuItem = char.ToUpper(IOConsole.GetChar("\nPlease pick an item: "));
        while (menuItem != 'C'
            && menuItem != 'R' && menuItem != 'Q' && menuItem != 'U' && menuItem != 'D' && menuItem != 'S' && menuItem != 'L' && menuItem != 'F' && menuItem != 'P' && menuItem != 'T')
        {
            Console.WriteLine("\nError - Invalid menu item");
            DisplayMenu();
            menuItem = char.ToUpper(IOConsole.GetChar("\nEnter option or M for menu:"));
        }
        return menuItem;
    }

    static void DisplayMenu()
    {
       Console.WriteLine("C-> Create Contacts");
       Console.WriteLine("R-> Remove Contacts");
       Console.WriteLine("U-> Update Contacts");
       Console.WriteLine("D -> Load data from file");
       Console.WriteLine("S-> Save data to file");
       Console.WriteLine("L-> View sorted by last name");
       Console.WriteLine("F-> View sorted by first name");
       Console.WriteLine("P-> View by partial name search");
       Console.WriteLine("T-> View by contact type");
       Console.WriteLine("Q-> Quit");
    }

    //Routes to the appropriate process routine based on the user menu choice
    static void ProcessMenuItem(Char menuItem, List<Contact> contactList)
    {
        switch (menuItem)
        {
            case 'C':
                createContact();
                break;
            case 'R':
                Console.WriteLine("enter the contact name To delete");
                removeContact(contactList, Console.Read().ToString() );
                    break;
            case 'U':
                updateContact(contactList);
                break;
            case 'D':
                LoadFromFile();
                break;
            case 'S':
                saveToFile();
                break;

            case 'L':
                sortByLastName(contactList);
                break;
            case 'F':
                sortByFirstName(contactList);
                   break;
            case 'P':
                   DisplayList(contactList);
                   break;
            case 'T':
                   sortByContactType();
                   break;
            case 'Q':

                   break;

        }                   
    }
// allows the user to update a contact
     public static void updateContact(List<Contact> contactList)
    {
        char answer, answer2, answer3, answer4,asnwer5;
        String newFirstName = " ";
        String newLastName = " ";
        String newPhoneNumber = " ";
        String newEmailAddress = " ";
        //Type newContactType = Contact.ContactTypes;


         Console.WriteLine("\nUpdate Contact:{1}" , contactList); 


        foreach (Contact obj in contactList)
        {

                answer = IOConsole.GetChar("\nEdit FirstName?: Y/N: ");


                if (answer.Equals('Y') || answer.Equals('y'))
                {
                    newFirstName = IOConsole.Getstring("\nUpdate Contact: please enter the contact's new first name: ");
                    obj.GetFirstName = newFirstName;
                    break;
                }

                else if (answer.Equals('N') || answer.Equals('n'))
                {

                }


                    answer2 = IOConsole.GetChar("\nEdit LastName?: Y/N: ");


                    if (answer2.Equals('Y') || answer.Equals('y'))
                    {
                        newLastName = IOConsole.Getstring("\nUpdate Contact: please enter the contact's new last name: ");
                        obj.GetFirstName = newLastName;
                        break;
                    }

                    else if (answer2.Equals('N') || answer.Equals('n'))
                    {



                    }


                    answer2 = IOConsole.GetChar("\nEdit EmailAddress?: Y/N: ");


                    if (answer2.Equals('Y') || answer.Equals('y'))
                    {
                        newEmailAddress = IOConsole.Getstring("\nUpdate Player: please enter the contact's new emailaddress) name: ");
                        obj.GetFirstName = newEmailAddress;
                        break;
                    }

                    else if (answer2.Equals('N') || answer.Equals('n'))
                    {



                    }


                    answer3 = IOConsole.GetChar("\nEdit phonenumber?: Y/N: ");


                    if (answer3.Equals('Y') || answer.Equals('y'))
                    {
                        newPhoneNumber = IOConsole.Getstring("\nUpdate Contact: please enter the contact's new phonenumber: ");
                        obj.GetFirstName = newPhoneNumber;
                        break;
                    }

                    else if (answer3.Equals('N') || answer.Equals('n'))
                    {



                    }


                    answer4 = IOConsole.GetChar("\nEdit ContactType?: Y/N: ");


                   /* if (answer4.Equals('Y') || answer.Equals('y'))
                    {
                        newContactType = IOConsole.Getstring("\nUpdate Player: please enter the contact's new type: ");
                        obj.GetFirstName = newContactType;
                        break;
                    }

                    else if (answer4.Equals('N') || answer.Equals('n'))
                    {



                    }*/


        }

    }

1 个答案:

答案 0 :(得分:4)

你能改变一下吗?

Console.WriteLine("\nUpdate Contact:{1}" , contactList);

要:

Console.WriteLine("\nUpdate Contact:{0}" , contactList);

在格式化字符串时, 必须以0开头,按参数列表给出的顺序。