473,326 Members | 2,061 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,326 software developers and data experts.

Cannot modify the return value of...

I came across this problem at work, and now at home... I've got an object
that stores its position in a Vector3 object.

Looks like this:

public class Object3D
{
Vector3 _position;
public Vector3 Position
{
get { return _position; }
}
}

But when I try setting a field, like this:

Object3D dan = new Object3D();
dan.Position.X = 10;

I get the error in the subject, because a Vector3 is a value type.

Given I might just want to increase the X position, is there any way I can
change this value, without setting it to a new Vector3 and passing the other
two fields back in?
--
Daisy The Cow
Now playing: Toploader - Time Of My Life
Nov 15 '05 #1
4 5393
Why would you want to do that? Since Vector3 is a value type, "return
_position" will return the value of object _position. So, you won't be able
to make changes at that point.

You can try making Vector3 a class (so, it becomes Pass-by-reference)

VJ

"Daisy" <da***@nospam.oops> wrote in message
news:on***************@newsfep4-winn.server.ntli.net...
I came across this problem at work, and now at home... I've got an object
that stores its position in a Vector3 object.

Looks like this:

public class Object3D
{
Vector3 _position;
public Vector3 Position
{
get { return _position; }
}
}

But when I try setting a field, like this:

Object3D dan = new Object3D();
dan.Position.X = 10;

I get the error in the subject, because a Vector3 is a value type.

Given I might just want to increase the X position, is there any way I can
change this value, without setting it to a new Vector3 and passing the other two fields back in?
--
Daisy The Cow
Now playing: Toploader - Time Of My Life

Nov 15 '05 #2
"Vijaye Raji" <no************@hotmail.com> wrote in message
news:Ua********************@comcast.com...
Why would you want to do that? Since Vector3 is a value type, "return
_position" will return the value of object _position. So, you won't be able to make changes at that point.

You can try making Vector3 a class (so, it becomes Pass-by-reference)


Vector3 is a DirectX class, not mine :-)

I did think about wrapping another around it, but it seems a bit silly...
Vector3 is probably a value type for a reason (even though with boxing and
unboxing, I don't see any advantage?!)
--
Daisy The Cow
Now playing: The Cardigans - Lovefool
Nov 15 '05 #3
Hi Daisy,

Since Vector3 is a value type, I believe a *copy* of the "_position" is
actually returned. Since there is no sense in modifying the copy - it won't
be persisted anywhere - the compiler gives you the error in question.

You should introduce a method like Move(xOffset, yOffset, zOffset) or
MoveX(offset) instead, or indeed switch to a reference type to store the
position. The latter approach, however, is more error prone as you have no
control on how and when the object position is modified.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Daisy" <da***@nospam.oops> wrote in message
news:on***************@newsfep4-winn.server.ntli.net...
I came across this problem at work, and now at home... I've got an object
that stores its position in a Vector3 object.

Looks like this:

public class Object3D
{
Vector3 _position;
public Vector3 Position
{
get { return _position; }
}
}

But when I try setting a field, like this:

Object3D dan = new Object3D();
dan.Position.X = 10;

I get the error in the subject, because a Vector3 is a value type.

Given I might just want to increase the X position, is there any way I can
change this value, without setting it to a new Vector3 and passing the other two fields back in?
--
Daisy The Cow
Now playing: Toploader - Time Of My Life


Nov 15 '05 #4
You can expose two other properties, say, PositionX and PositionY that would
directly modify the members of Vector3's position member.

Or some method like SetPosition(x, y)...

Sorry, I've run out of ideas and I guess you'd already come up with these
before...

vJ

"Daisy" <da***@nospam.oops> wrote in message
news:bq**********@linux01.dannytuppeny.com...
"Vijaye Raji" <no************@hotmail.com> wrote in message
news:Ua********************@comcast.com...
Why would you want to do that? Since Vector3 is a value type, "return
_position" will return the value of object _position. So, you won't be

able
to make changes at that point.

You can try making Vector3 a class (so, it becomes Pass-by-reference)


Vector3 is a DirectX class, not mine :-)

I did think about wrapping another around it, but it seems a bit silly...
Vector3 is probably a value type for a reason (even though with boxing and
unboxing, I don't see any advantage?!)
--
Daisy The Cow
Now playing: The Cardigans - Lovefool

Nov 15 '05 #5

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...
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 -------------------------------------------------------------------------------- ...
7
by: John Grandy | last post by:
make a call to XML Web Service WebMethod ... returns object myArray with no error ... myArray contains objects of type StringKeyStringValue runtime error occurs on accessing properties of...
1
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
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...
2
by: Hvid Hat | last post by:
class Sprite { private Vector2 position; public Vector2 Position { get { return position; } set { position = value; } } ... }
6
by: Ramesh | last post by:
Hello, I am using the ofstream class to create a text file with keys and values like: Key1=Value10 Key2=Value15 Key3=Value20 In case I need to set a new value for Key2, say value50 - I am...
1
mikek12004
by: mikek12004 | last post by:
In top of my index I include funcs.php which is : <?php function GetCartId() { //if user logged on use username as identification, else use coockie for anonymous user if ($_SESSION==1) {...
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"> <!--
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.