Cannot modify the return value of...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daisy

    #1

    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


  • Vijaye Raji

    #2
    Re: Cannot modify the return value of...

    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" <daisy@nospam.o ops> wrote in message
    news:onuxb.515$ Hy3.432@newsfep 4-winn.server.ntl i.net...[color=blue]
    > 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[/color]
    other[color=blue]
    > two fields back in?
    >
    >
    > --
    > Daisy The Cow
    > Now playing: Toploader - Time Of My Life
    >
    >[/color]


    Comment

    • Daisy

      #3
      Re: Cannot modify the return value of...

      "Vijaye Raji" <nospam_vganesh @hotmail.com> wrote in message
      news:Ua2dnVSDo6 S8B1uiRVn-ig@comcast.com. ..[color=blue]
      > 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[/color]
      able[color=blue]
      > to make changes at that point.
      >
      > You can try making Vector3 a class (so, it becomes Pass-by-reference)[/color]

      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


      Comment

      • Dmitriy Lapshin [C# / .NET MVP]

        #4
        Re: Cannot modify the return value of...

        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

        Bring the power of unit testing to VS .NET IDE

        "Daisy" <daisy@nospam.o ops> wrote in message
        news:onuxb.515$ Hy3.432@newsfep 4-winn.server.ntl i.net...[color=blue]
        > 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[/color]
        other[color=blue]
        > two fields back in?
        >
        >
        > --
        > Daisy The Cow
        > Now playing: Toploader - Time Of My Life
        >
        >[/color]

        Comment

        • Vijaye Raji

          #5
          Re: Cannot modify the return value of...

          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" <daisy@nospam.o ops> wrote in message
          news:bq6tm5$i9p $1@linux01.dann ytuppeny.com...[color=blue]
          > "Vijaye Raji" <nospam_vganesh @hotmail.com> wrote in message
          > news:Ua2dnVSDo6 S8B1uiRVn-ig@comcast.com. ..[color=green]
          > > 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[/color]
          > able[color=green]
          > > to make changes at that point.
          > >
          > > You can try making Vector3 a class (so, it becomes Pass-by-reference)[/color]
          >
          > 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
          >
          >[/color]


          Comment

          Working...