2



Im experiencing some weird behaviour of some simple code i'm using to store a 2D position of an object.
I've created a class called SizeD containing a Width and Height as Double.

CODE:

public class SizeD
{
    internal Double Width;
    internal Double Height;
    /// <summary>
    /// Creates a new instance of SizeD
    /// </summary>
    public SizeD(Double Width, Double Height)
    {
        this.Width = Width; this.Height = Height;
            return;
    }
}

This is pretty straight foward and i didn't expect anything special to happen from this class.
There's also a static array with some of these predefined classes for easier use.

CODE:

internal static SizeD[] PaperTypeSize =  //Converts CM to points.
{
    new SizeD(8.5 * 72, 11.0 * 72),
    new SizeD(8.5 * 72, 14.0 * 72),
    new SizeD(21.0 * (72 / 2.54), 29.7 * (72 / 2.54))
};

The problem that i'm experiencing is that the value within the SizeD often Flip at runtime.

1: Width = Width and Height = Height.
2: Width = Height and Height = Width.
This also happens with a repeating pattern (1,2,1,2,1,2) and so on.

Can somone please explain to me what's happening here?
Thanks for your time!

EDIT: It happens mostly when trying to read this value SizeD(21.0 * (72 / 2.54), 29.7 * (72 / 2.54)).

Noctis
  • 11,507
  • 3
  • 43
  • 82
Jeroen Vorsselman
  • 803
  • 1
  • 9
  • 19
  • 1
    Please show an [example](http://sscce.org/). – Tim S. Nov 04 '13 at 14:54
  • 2
    is anyone else assigning `Height` or `Width`? try making the `private`. – Daniel A. White Nov 04 '13 at 14:54
  • Look at all places where you are assigning to members of those objects or to array elements. – usr Nov 04 '13 at 14:56
  • Use read-only properties for accessing the Width and Height fields (this way you can also debug who is reading them, or who is writing them if it needs to be read-write). Avoid naming parameters the same as members. It's confusing. – PMF Nov 04 '13 at 15:06
  • Sounds like a floating point inconsistency to me : (http://stackoverflow.com/questions/13290758/floating-point-inconsistency-between-expression-and-assigned-object) Have you tried 21.0d * (72 / 2.54d), 29.7d * (72 / 2.54d) ? – Yiyuan Lee Nov 05 '13 at 08:13
  • First, what's the `return` in the constructor for? Second, why do you use magic numbers? have some CONSTANTS like: `NameThatMakesSense = 72` and use them for assignment... remember to be [DRY](http://en.wikipedia.org/wiki/Don't_repeat_yourself) :) – Noctis Nov 05 '13 at 08:15
  • The `return` in the constructor is superfluous. – Rik Nov 05 '13 at 08:18

1 Answers1

0

Make your fields readonly by using properties:

public class SizeD
{
    public double Width { get; private set; };
    public double Height { get; private set; };

    /// <summary>
    /// Creates a new instance of SizeD
    /// </summary>
    public SizeD(double width, double height)
    {
        this.Width = width;
        this.Height = height;
    }
}

Now, your code can only create read-only instances. Check where you get compile errors. One of those places is buggy and changes values incorrectly. Consider a design where the size is immutable, this will prevent those bugs. It's not always appropriate, but that is something only you can decide based on your use of this class in your program.

nvoigt
  • 75,013
  • 26
  • 93
  • 142