Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing value of List<Point> item

Tags:

c#

list

I'm making (school) application for drawing curves. I set the points by mouse clicking and add their positions to list of vertices. Now I'm working on moving of points using actions on mouse down and mouse up. On mouse down I find out if the position of mouse is in small square (4x4 px) around any vertex in list of vertices and then on mouse up I want to change coordinates of vertice to the coordinates where I raised the mouse button up. But I hit the problem with List cause visual studio says that List items couldn't be changed cause it's not the variable. How can I solve this?

List<Point> vertices = new List<Point>(); //list of vertices
void canvas_MouseUp(object sender, MouseEventArgs e) {
    if (!move) return; //if moving is off returns
    vertices[indexOfMoved].X = e.X; //change X position to new position
    vertices[indexOfMoved].Y = e.Y; //change Y position to new position
    indexOfMovedLabel.Text = "Moved: ?";
}

Problem:

Error 1 Cannot modify the return value of 'System.Collections.Generic.List.this[int]' because it is not a variable

like image 746
user1097772 Avatar asked Jan 17 '23 14:01

user1097772


1 Answers

This is because the Point is a struct and not an object. You can think of structs as grouped values.

So when you access vertices[indexOfMoved] you get a copy of what's in the list, not the actual 'object'.

You can do it like this:

vertices[indexOfMoved] = new Point { X = e.X, Y = e.Y };
like image 114
Davio Avatar answered Jan 24 '23 13:01

Davio