Solved

invalid casting exception

Posted on 2014-09-10
3
125 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now