views:

128

answers:

2

In my C#.Net WinForm app in VS2005, I have a NumericUpDown control. In the designer, I set the Minimum property of the NumericUpDown to -65535.

In the form's .designer.cs code, I expected to see this:

this.myNumericUpDown.Minimum = -65535;

but instead I get:

    this.myNumericUpDown.Minimum = new decimal(new int[] {
    65535,
    0,
    0,
    -2147483648});

I know what this means from looking up the int32[] decimal constructor, but why does the designer use that form instead of putting the value I entered, or even using the int32 decimal constructor?

A: 

It might be some premature optimization forcing the exponent part of the decimal to 0 for faster comparison with other decimals without having to do any scaling, or for faster increment/decrement operations.

this.myNumericUpDown.Minimum = new decimal(new int[] {
    65535,
    0,
    0,
    -2147483648}); // -2147483648 == 10000000000000000000000000000000b
                   // => only sign bit is set
Florian Doyon
Once the decimal is constructed, it will have the same internal representation whether it was created using the constructor that takes (Int32[]) or the constructor that takes (Int32) So I still don't understand why Visual Studio designer chooses to create the decimal using a less human-readable constructor
JeffH
Most of the time, the designer code isn't read by humans, so that wasn't a concern for the VS developers.
Pedro
@Pedro - VS developers had to choose the (Int32[]) form of the constructor over the (Int32) form. The (Int32[]) form is more verbose, thereby more prone to error. My question is: *why* would the VS developers choose to use the more complex but apparently equivalent constructor?
JeffH
+2  A: 

In theory the designer could store your original input text. Yet since the visual designer can also modify these values (i.e. Left, Top, etc) it would have to reconcile this storage against the control's values during serialization. This is presumably one reason why the designer does not store the text values entered, rather it takes your text and converts it to a value that can be placed into the control. When it needs to serialize it just reads the control's properties and writes them. There would little point to interrogate the details of the decimal prior to storage just to optimize which constructor to use. It needs to handle all possible values of the decimal type, thus the int[] provides a round-trip reliable storage mechanic for any value a decimal can represent.

BTW, the following outlines the internal structure of the decimal:

  • bits is a four-element array of 32-bit signed integers.
  • bits [0], bits [1], and bits [2] contain the low, middle, and high 32 bits of the 96-bit integer number.
  • bits [3] contains the scale factor and sign, and consists of following parts:
    • Bits 0 to 15, the lower word, are unused and must be zero.
    • Bits 16 to 23 must contain an exponent between 0 and 28, which indicates the power of 10 to divide the integer number.
    • Bits 24 to 30 are unused and must be zero.
    • Bit 31 contains the sign; 0 meaning positive, and 1 meaning negative.

Update:

Would you explain "...since the visual designer can also modify these values, ... it would have to reconcile this storage against the control's values during serialization..." in more detail?

Sure. I was just trying to address the literal interpretation of your question "why does the designer use that form instead of putting the value I entered". Basically all I'm saying is that the designer doesn't keep track of the exact textual value you entered "-65535". Instead, it converts it to a decimal with something like decimal.Parse(...). Then it places this decimal into the control's value, in this case NumericUpDown.Minimum. Afterwords it serializes the state of the control in the xxxx.Designer.cs file by reading each property value. Thus it no longer has a copy of the original text "-65535" you entered, but only the resulting decimal value.

The part referring to "reconcile this storage..." was to simply to point out that it would be difficult to not only track the original text input by a user, but also detect if that value had been changed by some other means that the original text input.

Sorry if that is still confusing I'm having trouble describing it. English is my 5th language, right behind C#, C++, SQL, and JScript all of which are much more explicit in meaning than English :)

csharptest.net
Would you explain "...since the visual designer can also modify these values, ... it would have to reconcile this storage against the control's values during serialization..." in more detail?
JeffH