Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

invalid casting exception

Posted on 2014-09-10
3
Medium Priority
?
159 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 143

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 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 1000 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 1000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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!
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

916 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