InvalidCastException by indexing result in ArrayList

I have an ArrayList filled with a bunch of Points, and I want to iterate over them, so I use this code:

for (int i = 0; i < currentClicks.Count; i++) { if (i > 0) // Skip the first click { clickPos = currentClicks[i]; prevPos = currentClicks[i - 1]; } } 

and I get this error in the clickPos and prevPos :

 Cannot implicitly convert type 'object' to 'System.Drawing.Point'. An explicit conversion exists (are you missing a cast?) 

Why is this? I have clickPos and prevPos defined like this:

 private System.Drawing.Point clickPos; private System.Drawing.Point prevPos; 

Edit

When I comment out the lines of clickPos and prevPos and add

 MessageBox.Show(currentClicks[i].GetType().ToString()); 

the message field says System.Drawing.Point

+4
source share
8 answers

You should use a generic List<Point> instead of an ArrayList .

If you insist on using an ArrayList, you will need to drop objects to Point when retrieving them:

 clickPos = (Point)currentClicks[i]; 
+6
source

Try the following:

 for (int i = 1; i < currentClicks.Count; i++) { clickPos = (System.Drawing.Point)currentClicks[i]; prevPos = (System.Drawing.Point)currentClicks[i - 1]; } 

A better solution would be to use a general list, List<Point> , then you won’t need a cast at all.

+4
source

ArrayLists not strongly typed. They are mainly IList of object .

Because of this, you need to point out your references to the elements of the array to the type that you know.

  clickPos = (Point)currentClicks[i]; prevPos = (Point)currentClicks[i - 1]; 

I would highly recommend using List<Point> rather than ArrayList . This will give you a strongly typed Points list and eliminate the need to use your links.

A generic List<T> also offers the same functionality as an ArrayList , but usually with better performance. From the documentation:

Performance recommendations

When deciding whether to use a List or an ArrayList Class, both of which have similar functionality, remember that the List class works best in most cases and is type safe. If a type reference is used for a type T list of classes, the behavior of the two classes is identical.

+3
source

Why don't you start with 1?

 for (int i = 1; i < currentClicks.Count; i++) { clickPos = (System.Drawing.Point)currentClicks[i]; prevPos = (System.Drawing.Point)currentClicks[i - 1]; } 
+2
source

As for your editing: you know that ArrayList contains points, and the runtime knows that ArrayList contains points (hence the result .GetType() ), but the compiler does not know that the arrailist holds points. You need a cast to satisfy the compiler.

But in fact, the only correct way to fix this is to change it to a generic List<Point> .

+1
source

Nick code will work, or you can try:

ArrayList<System.Drawing.Point>

ArrayList implements IEnumerable, which means that it always returns System.Object. ArrayList<T> implements IEnumerable and will always return T.

0
source

An ArrayList contains references to objects, and you are trying to implicitly drop these objects in Points. You can explicitly specify, as Nick shows, or use a generic List<T> , which I would recommend:

 List<Point> currentClicks = new List<Point>(); // add your points 

Using a common list (added to .NET in version 2.0, described here ) allows you to work with objects in a much larger number of types, in a safe way, without application at run time. If you are working with .NET 2.0 or higher, you should never use ArrayList , Hashtable or any other non-generic constructs.

0
source

If you are not stuck on .NET 1.1 or have no control over creating a list, you will be better off with List<Point>

ArrayList requires you to use exclusion roles to access members if you don't want to use them just as an object, so you need this

 clickPos = (Point)currentClicks[i]; prevPos = (Point)currentClicks[i - 1]; 
0
source

Source: https://habr.com/ru/post/1301295/


All Articles