Solved

invalid casting exception

Posted on 2014-09-10
3
133 Views
Last Modified: 2014-09-11
I have the following code and I'm getting an invalid casting exception on i on the for loop can someone explain>?

 class MyObject : IComparable
    {
        public int ID { get; set; }

        public int CompareTo(object obj) {
            MyObject obj1 = obj as MyObject;
            return this.ID.CompareTo(obj1.ID);
        
            }
    }

Open in new window


 static void Main(string[] args)
        {

            //int[] myset = new int[5] { 1, 3, 4, 5, 6 };

         //   ArrayList myArray = new ArrayList();
         //   myArray.Add(2);
         //   myArray.Add(10);
         //   myArray.Add(5);
         //   myArray.Add(3);

            ArrayList myList = new ArrayList();
            myList.Add(new MyObject() { ID = 4 });
            myList.Add(new MyObject() { ID = 200 });
            myList.Add(new MyObject() { ID = 10 });
            myList.Sort();

            foreach (int i in myList) {
                Debug.WriteLine(i.ToString());
           
            }
0
Comment
Question by:yguyon28
3 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40316278
it must be like this:
 public int CompareTo(MyObject obj)
 {
       return this.ID.CompareTo(obj.ID);
 }
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 250 total points
ID: 40316280
foreach (int i in myList)
myList is a list of MyObject not of int - hence the error

it should be
foreach (MyObject i in myList)
but then I don't know what you want to do with the line after (Debug.WriteLine)
0
 
LVL 12

Accepted Solution

by:
trinitrotoluene earned 250 total points
ID: 40316304
you probably want to do this:

 foreach (MyObject i in myList) {
                Debug.WriteLine(i.ID.ToString());
}

Open in new window

)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question