Error: Cannot modify the return value c#
This is because Point
is a value type (struct
).
Because of this, when you access the Origin
property you're accessing a copy of the value held by the class, not the value itself as you would with a reference type (class
), so if you set the X
property on it then you're setting the property on the copy and then discarding it, leaving the original value unchanged. This probably isn't what you intended, which is why the compiler is warning you about it.
If you want to change just the X
value, you need to do something like this:
Origin = new Point(10, Origin.Y);
CS1612 Cannot modify the return vaue because it is not a variable
you are trying to make assignment to CmdTest.Location
. check my fix
CmdTest.Location = new Point(
CmdTest.Location.X + 20, CmdTest.Location.Y);
Cannot Modify Return Value because It is not a Variable
There is probably never going to be a "shortcut" for this since it's a feature of the fundamental difference between value types and reference types.
Cannot modify the return value error when modifying content of List T
Welcome to the wonderful world of evil mutable structs!
Change ThreadCheck
to a class
and everything will work fine.
Impossible to modify the return value of 'Selectable.Colors' because it's not a variable
The root cause of the error is that ColorBlock
is a struct
, i.e. a value type.
When you access colors
property of ButtonLeft
, a copy of the struct
is made, i.e. ButtonLeft.colors
is a copy of ColorBlock
inside ButtonLeft
. Although assignments to properties of colors
could theoretically succeed, they would have no effect on the colors
inside ButtonLeft
, which is certainly a programming error. That is why the language triggers an error, prohibiting the assignment.
That is why you need to use ClolrBlock
in the way described by the documentation:
ColorBlock cb = ButtonLeft.colors;
cb.normalColor = newColor;
button.colors = cb;
Cannot modify the return value of a collection of rectangles
Rather than working directly with Rectangles, how about adding a BodyPart class, with some manipulation methods:
public class BodyPart
{
private Rectangle rectangle;
public BodyPart(Rectangle rectangle)
{
this.rectangle = rectangle;
}
public void MoveY(int value)
{
rectangle.Y += value;
}
public void MoveX(int value)
{
rectangle.X += value;
}
}
public class Snake
{
public List<BodyPart> Parts = new List<BodyPart>();
}
public class AppSnake
{
public void Run()
{
var snake = new Snake();
snake.Parts.Add(new BodyPart(new Rectangle(760, 25, 8, 8)));
snake.Parts[0].MoveY(-10);
}
}
Related Topics
Parameters Passed by Reference Come Back Garbage Using P/Invoke
Get the Generated SQL Statement from a SQLcommand Object
How to Read a File Even When Getting an "In Use by Another Process" Exception
Cross-Thread Operation Not Valid
Send a File via Http Post with C#
List of Timezone Ids for Use with Findtimezonebyid() in C#
Using Stored Procedure Output Parameters in C#
C# Test If User Has Write Access to a Folder
Are C# Uninitialized Variables Dangerous
How to Autheticate to Ibm Mq C# with Tls-Certificate
When to Use Cast() and Oftype() in Linq
Launching an Application (.Exe) from C#
How to Represent 0.1 in Floating Point Arithmetic and Decimal
How to Make Something That Catches All 'Unhandled' Exceptions in a Winforms Application
Efficient Way to Remove All Whitespace from String
How to Create a Dynamic Button Click Event on a Dynamic Button