Question

I'm working on a homework assignment, everything works except the binary search. When the program executes, I get the following error in Friend temp = (Friend)o; of the IComparable: InvalidCastException was unhandled by user code. Unable to cast object of type 'System.String' to type 'FriendList.Friend'. I could use help to pinpoint where I'm misguided. Thanks in advance.

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);               
        }
    }
}
Was it helpful?

Solution

The issue is that you are trying to do a binary search on an array of type Friend to look for a String value. So when your ICompare implementation tries to cast the name to type friend it throws the exception you are seeing. To correctly use the ICompare you would need to create a new temporary friend object to pass into the binary search but just set the name like so:

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

OTHER TIPS

When you're calling

 x = Array.BinarySearch(friends, findname);

you're passing findname, it is string. Here:

Friend temp = (Friend)o;

you're trying to cast this string to the Friend class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top