473,230 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

Cannot modify the return value... because it is not a variable

class Sprite
{
private Vector2 position;
public Vector2 Position
{
get { return position; }
set { position = value; }
}
...
}

class Vehicle : Sprite
{
public void Update()
{
Position.X += velocity.X; // <--- Cannot modify the return value
}
}

How to fix? And yes, I'm not an experienced C#-programmer :-)
Jun 27 '08 #1
2 13395
Vector2 is a struct, yes?

The problem is that when you get a struct, you actually get a *clone*
(a blit). If the compiler let you do this, you would edit the clone
and then discard the clone - i.e. nothing useful. I also recommend not
making structs "mutable" - this leads to too many problems to count.
But anyway, I would do something like:

Vector2 old = Position;
Position = new Vector2(old.X + velocityX, ...);

or perhaps, if velocity is also a vector (and is your code), implement
the + operator for Vector2, and use:

Position += velocity;

which is much cleaner. If it isn't your code, you could write a
separate Add method, perhaps as an "extension" method:

Position = Position.Add(velocity);

Marc
Jun 27 '08 #2
Hvid Hat wrote:
class Sprite
{
private Vector2 position;
public Vector2 Position
{
get { return position; }
set { position = value; }
}
...
}

class Vehicle : Sprite
{
public void Update()
{
Position.X += velocity.X; // <--- Cannot modify the return value
}
}

How to fix? And yes, I'm not an experienced C#-programmer :-)
This code isn't trying to do what you think it's trying to do...

Vector2 is a struct, so your call in Update first *copies* Position onto
the stack, adds velocity.X to the new Vector2's 'X' and then throws it
away. The original value was never modified.

The C# compiler catches your mistake in this case (phew!). You instead
need to do:

Position = new Vector2(Position.X+velocity.X, Position.Y);

The difference between value and reference types is fundamental in C# -
the C# specification describes that difference immediately after "hello,
world"

See section 8.2 of the C# specification for more information:

http://www.ecma-international.org/pu...T/Ecma-334.pdf

Alun Harford
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Doug Jordan | last post by:
I am fairly new to Python. This should be an easy answer but I cannot get this to work. The code is listed below. I know how to do this in C, Fortran, and VB but it doesn't seem to work the same...
20
by: CoolPint | last post by:
While I was reading about const_cast, I got curious and wanted to know if I could modify a constant variable through a pointer which has been "const_cast"ed. Since the pointer would be pointing to...
4
by: Markus Dehmann | last post by:
I guess this is a kind of newbie question (since most pointer questions are newbie questions). In the program below, modify(string* s) is supposed to change the content that s points to. But the...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
6
by: Håkan Johansson | last post by:
error CS0445: Cannot modify the result of an unboxing conversion I'm quite new to C# and can't really see how to get rid of the above error. I have an ArrayList instance to which I add two...
4
by: sirjohnofthewest | last post by:
If I possessed the power to sway the mind of every user in the world to delete all forms of Internet Explorer I would die a happy man. Hi guys, I frequently visit this site to get answers to my...
3
by: pinko1204 | last post by:
My Update function cannot successful update to sql table even don't have any error. Please help to check .....thx PHP1 <?php require_once 'header.php'; ?> <style type="text/css"> <!--
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.