0

I got an arraylist of rectangles and I want to edit the X & Y values of all of them.

I tried using a foreach loop

        foreach (Rectangle rect in rectangles)
            rect.X += 1;

But this wouldn't work as it's read-only, so I tried a regular for loop

        for (int i = 0; i < rectangles.Count; i++)
            rectangles[i].X += 1;

And for some reason this wouldn't work either, because rectangles[i] just doesn't have any of rectangle's methods.

Then I stumbled across a post somewhere on stackoverflow about how to call methods of elements of an arraylist. And I haven't been able to find examples of this, so I hope someone can clear this up.

(Unit.unitArray[selectedUnit] as MyClass).DisplayUnitAttributes()

But I have no idea how to put this to use, I don't understand what Unit is supposed to be replaced with, and I'm guessing MyClass would be Rectangle...

Any help is appreciated!

TLDR: I want to iterate over an arraylist with rectangles, and edit the X & Y values of them.

SJ19
  • 1,933
  • 6
  • 35
  • 68
  • Could you sum up the differences for me? I'm used to Java where ArrayList is an interface of List. – SJ19 Oct 04 '15 at 06:48
  • @GrantWinney Tried for (int i = 0; i < rectangles2.Count; i++) rectangles2[i].X += 1; where rectangles2 is a List, doesn't work because "Return value is not a variable"... – SJ19 Oct 04 '15 at 06:54
  • @M.kazemAkhgary That doesn't work, and I don't know what a struct is. – SJ19 Oct 04 '15 at 07:02
  • @M.kazemAkhgary Btw, I'm using C#'s Rectangle class, I just want a way to edit values of it in an arraylist, I'm getting linked everywhere, it's not helping me. Can you please just answer my question. – SJ19 Oct 04 '15 at 07:11
  • I like how everyone is deleting their answers and comments, please only post if you actually know what you're doing. – SJ19 Oct 04 '15 at 07:14
  • I know what am i doing. i just want to clear up everything. i dont like extra noise! :) – M.kazem Akhgary Oct 04 '15 at 07:19
  • @M.kazemAkhgary You literally deleted your own comment. You wouldn't do that unless you knew it was redundant. – SJ19 Oct 04 '15 at 07:21

2 Answers2

1

According to the error you have got. "Return value is not a variable"

Rectangle is not a class. its an struct which is not reference type so you have to assign new value into it.

List<Rectangle> rectangles = new List<Rectangle>();

// rectangles.Add(x); make your list here

for (int index = 0; index < rectangles.Count; index++)
{
    Rectangle r = rectangles[index];
    r.x += 1;
    rectangles[index] = r;
}
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
0

It's act like this because ArrayList is not typed. It's better to use a List<T> instead (where T is your class, in your example it will be a Rectangle). Here is MSDN link.

You can't modify object this way in the foreach loop. More info on this SO question.

About this example: (Unit.unitArray[selectedUnit] as MyClass).DisplayUnitAttributes() It's called casting. You can read more about this on this SO question. So in your code it should looks like this: (rectangles[i] as Rectangle).X += 1;. You also should check if result of this casting is not null etc. But better read previous link to understand this.

Community
  • 1
  • 1
Grzegorz Piotrowski
  • 913
  • 1
  • 10
  • 12
  • I know what casting is, and I tried ---- for (int i = 0; i < rectangles2.Count; i++) (rectangles[i] as Rectangle).X += 1; ---- but it doesn't work. – SJ19 Oct 04 '15 at 07:15
  • Ah, okey, your Rectangle is a struct, not a class. Sorry! Second answer is good then ;) – Grzegorz Piotrowski Oct 04 '15 at 07:25