1

C# Won't allow me to do this.

foreach (Point point in filledPoints)
{
    point.X = 0;
} 

filledPoints is a List<Point> It gives me a compiler error: "filledPoints is a foreach iteration variable, so the associated members can't be edited" (Sorry, the message is in german and im bad at translating). But this works:

foreach (Point point in filledPoints)
{
    Point point2 = point;
    point2.X = point2.X / oldSize.Width * Size.Width;
}   

Why does this not work and is there a more elegant way to bypass it?

10
  • 1
    The error message tells you exactly why it doesn't work. Commented Dec 1, 2017 at 20:02
  • It just tells me that it doesn't work but not why @Servy Commented Dec 1, 2017 at 20:03
  • 2
    I doubt that it tells you That it doesn't work also what line does it throw the error on..? do you know how to use the debugger..? if so set breakpoints and tell us the valid error Commented Dec 1, 2017 at 20:05
  • 2
    @KeineAhnung what version of Visual Studio/.NET/C# are you on? I cannot find the version that has " it doesn't work" as an error message. Commented Dec 1, 2017 at 20:09
  • 2
    @KeineAhnung ignore me, I was being facetious. To try and simiplify: the reason you are getting that error is because you are modifying the value inside a foreach (which you are not allowed to do). Normally with reference types its ok to modify properties but Point is not a reference type, its a struct. So when you are trying to modify a property, you are actually modifying the entire thing. Commented Dec 1, 2017 at 20:17

2 Answers 2

6

This is because Point is not a reference type (it's a struct not a class). So the point variable in the foreach is not actually a reference to the original Point, it is a copy. The compiler doesn't let you modify it because (I assume) it would be easy to think you're changing the original when you are not.

I'm not sure if there's a better way but you could get around this by doing something like this:

for (int i = 0; i < filledPoints.Count; i++)
{
    Point temp = filledPoints[i];
    temp.X = 10;
    filledPoints[i] = temp;
}
Sign up to request clarification or add additional context in comments.

3 Comments

That makes sense. Is there an alternative class or do I need to create it?
It's a bit messy but you could use a for loop and a temporary Point variable. I'll edit my answer with an example.
@Joeyrp don't remove the code, its the right way of doing it. I would hate for future readers to recreate the poor solution OP went with. As for OP, you should really listen to peoples advice instead of dismissing it.
1

@Cathal was close. You need to use a for loop like this:

for(int i = 0; i < points.Length; i++)
{
    //This copies the value of the point at the i index into a variable called point
    Point point = points[i];

    //Modifying the point.X property of the copy
    point.X = 0;

    //This replaces the point that is at the i index, with the new point that has the modified X property
    points[i] = point;
} 

Fiddle here

EDIT

I added some comments and additional code to my fiddle to illustrate why your foreach is not working the way you think it does.

13 Comments

That was the thing I've already dont with the foreach loop. Thank you but no more help is needed :D
@KeineAhnung I don't think your foreach loop will work the way you want it too. You might want to re-look at my answer.
A struct is a value type, so when you create a new variable it copies the existing struct values. Then you modify it and re-add it to the list. This is exactly what you need to do. You should not be making a class that is 1 to 1 with Point because you do not understand struct. I am trying to explain it to you but you are being stubborn. Look at the fiddle.
@KeineAhnung the answer you accepted it the right way too. Just dont create your own class. Use the struct
@macceturra I understand it now. Sorry!
|

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.