473,322 Members | 1,473 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,322 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 13409
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: 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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.