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.

7
  • Could you sum up the differences for me? I'm used to Java where ArrayList is an interface of List. Commented Oct 4, 2015 at 6:48
  • @GrantWinney Tried for (int i = 0; i < rectangles2.Count; i++) rectangles2[i].X += 1; where rectangles2 is a List<Rectangle>, doesn't work because "Return value is not a variable"... Commented Oct 4, 2015 at 6:54
  • @M.kazemAkhgary That doesn't work, and I don't know what a struct is. Commented Oct 4, 2015 at 7: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. Commented Oct 4, 2015 at 7:11
  • I like how everyone is deleting their answers and comments, please only post if you actually know what you're doing. Commented Oct 4, 2015 at 7:14

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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.
Ah, okey, your Rectangle is a struct, not a class. Sorry! Second answer is good then ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.