BinarySearch错误导致InvalidCastException

时间:2014-01-31 03:31:08

标签: c# binary-search

我正在做家庭作业,一切都有效,除了二元搜索。当程序执行时,我在Friend temp =(Friend)o中得到以下错误; IComparable:InvalidCastException未被用户代码处理。无法转换类型为' System.String'的对象输入' FriendList.Friend'。我可以用帮助来查明我误入歧途的地方。提前谢谢。

using System;

using System.Linq;

namespace FriendList
{
public static class FriendList
{        
    public static void Main()
    {
        var findname = "";            
            var friends = new Friend[8];

            for (var i = 0; i < 8; i++)
            {
                Console.Write("Enter a name (or type quit to skip): ");
                var name = Console.ReadLine();
                if (name.ToLower() == "quit")
                {
                    return;
                }

                Console.Write("Enter a phone number: ");
                var phone = Console.ReadLine();

                Console.Write("Enter a birth month number: ");
                var month = Console.ReadLine();

                Console.Write("Enter a birth day: ");
                var day = Console.ReadLine();

                Console.Write("Enter a birth year: ");
                var year = Console.ReadLine();

                friends[i] = new Friend
                {
                    Name = name,
                    Phone = phone,
                    Month = Convert.ToInt32(month),
                    Day = Convert.ToInt32(day),
                    Year = Convert.ToInt32(year)
                };
            }

                Array.Sort(friends);

                for (int x = 0; x < 8; ++x)
                {
                    Console.Write("{0}   {1}   {2}{3}{4}", friends[x].Name, friends[x].Phone, friends[x].Month, friends[x].Day, friends[x].Year);
                    Console.Write("\n");
                }

                Console.ReadKey();

                var findfriends = new Friend();
                Console.Write("Enter the name you want for info:");            
                findname = Console.ReadLine();

                for (int x = 0; x < 8; ++x)
                {
                    x = Array.BinarySearch(friends, findname);
                    Console.Write("{0}   {1}   {2}{3}{4}", friends[x].Name, friends[x].Phone, friends[x].Month, friends[x].Day, friends[x].Year);
                }
        }
    }


    class Friend : IComparable
    {
        public string Name { get; set; }
        public string Phone { get; set; }
        public int Month { get; set; }
        public int Day { get; set; }
        public int Year { get; set; }

        public Friend()
        { }

        int IComparable.CompareTo(Object o)
        {
            Friend temp = (Friend)o;
           return String.Compare(this.Name, temp.Name);               
        }
    }
}

2 个答案:

答案 0 :(得分:1)

问题是您正在尝试对类型为Friend的数组执行二进制搜索以查找String值。因此,当您的ICompare实现尝试将名称强制转换为类型friend时,它会抛出您看到的异常。要正确使用ICompare,您需要创建一个新的临时友元对象以传递给二进制搜索,但只需设置如下名称:

Array.BinarySearch(friends, new Friend(){ Name = findName });

答案 1 :(得分:0)

当你打电话时

 x = Array.BinarySearch(friends, findname);

你传给了findname,它是字符串。这里:

Friend temp = (Friend)o;

您正在尝试将此字符串转换为Friend类。