using a PointF type object in a Property

Anybody have any idea why I can't get/set a PointF variable that I've defined inside a class:

<snip>

private PointF _shoulderPoint;

internal PointF ShoulderPoint
{
get { return _shoulderPoint; }
set { _shoulderPoint = value; }
}

ShoulderPoint.X = (float)34.4
ShoulderPoint.Y = 25.5f

</snip>

both of the last two lines give me a compile error:

Error 1 Cannot modify the return value of 'XmlEditor.ArmInverseKinematics.ShoulderPoint' because it is not a variable C:\Alex\FlashDrive\Projects\RobotRenderer\RoboArmRobotRenderer1.0\Updated TRAL\Logitech Rumblepad version\XmlEditor\ArmInverseKinematics.cs 225 13 XmlEditor




Answer this question

using a PointF type object in a Property

  • chricosta

    Ah, I think I got you.

    So, in order to change the values of a value type object, a new object of that same type has to be instantiated

    Thanks,

    - Alex


  • Luciano Resende

  • IcezRiku

    That does clear things up a bit.

    I guess my only question is this. If I have the properties being set using:

    SholderPoint = new PointF(X, Y);

    inside a loop from a timer (about 100ms ticks) that is constantly running and recalculating the new PointF values, won't my loop end up quite inefficient, because the garbage collector cannot dispose of the objects quick enough and probably many other reasons that I'm not aware of




  • Mathieu_N

    hi,

    x and y are properties , i think you have to expose x and y in your application in properties as well like for example

    private static PointF _point;

    public static float myX

    {

    get { return _point.X; }

    set { _point.X = value; }

    }

    public static PointF MyPoint

    {

    get { return _point; }

    set { _point = value; }

    }

    hope this helps



  • ALDAPIXEL

    If you were to create a new PointF 10x a second your code wouldn’t be as efficient as it could be... but then is that really a problem

    Like so many possibilities of performance issues your best bet would be to do a little testing and determine some metrics for how bad creating 10 PointF’s a second every second would be for your application vs having a couple of properties that allow you direct access to the X and Y components. My guess... it wouldn’t be that bad.



  • Frank Osterberg

    You are partially correct with regards to what n0n4m3 said. You can easily change the properties of a value type as we see in shakalama’s example (which may be one way for you to fix your problem).

    The problem with setting a part of a value type without rebuilding it is with properties because to set the single value of the value type the property has to return the entire struct and then make available the individual value... when you try to set it the system doesn’t know what to do with this value as you are effectively setting (in your example) a float to a property that only accepts values of the type PointF.

    Does this make things a little more clear



  • donnbobhardy

    Hi,
    the reason why you can't do that is because PointF is a value type and so, the get property returns a copy of that struct. The compiler prevents you from changing that copy because it wouldn't work as you expected. To change that property you have to do it like this:

    SholderPoint = new PointF(10.0f, 20.0f);


  • using a PointF type object in a Property