Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

invalid casting exception

Posted on 2014-09-10
3
Medium Priority
?
155 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 44

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

670 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