0

Going through my code, I was hoping to remove instances where I do this:

Height = new Unit(doubleValue);

where Height was representing a value in pixels.

I didn't like having to say new everywhere, it felt like I should be able to just modify height's value, but it is read-only.

As such, I stumbled upon Unit.Pixel() and Unit.Percentage(). The percentage method takes a double, but the pixel method only handles integers.

Is there some big reason behind this, an oversight, or a misunderstanding on my part?

Is it standard to create a new unit every time I wish to change height's value?

EDIT: For reference,

Unit Constructor

Pixel Method

Sean Anderson
  • 27,963
  • 30
  • 126
  • 237

2 Answers2

2

The System.Web.UI.WebControls.Unit is a struct and therefore is immutable. You will need to create a new instance each time you want change the height value.

In web development, sizes are not always measured in pixels, there are many UnitTypes

Unit.Pixel(int n) // Pixels are represented as integer values.

I guess that the Constructor that accepts a double was provided for convenience.

You can do a simple test

var x = new Unit(5.0);
var y = Unit.Pixel(5);

Assert.AreEqual(x, y); // returns true
Community
  • 1
  • 1
Rohan West
  • 9,262
  • 3
  • 37
  • 64
  • But wait, if I have a Height with value 5 and type pixel, it is perfectly fine to say height = Unit.Pixel(10); This changes the value of height, no? – Sean Anderson Jul 13 '11 at 21:59
  • When I call new Unit(200.5, UnitType.Pixel); I am given an object which says its value is 200.5 pixels. Is this to say that if I use Unit.Pixel(200) I would see no visual differences? (Assuming no code is dependent on the double value and its only necessary for displaying to the screen) – Sean Anderson Jul 13 '11 at 22:05
0

You can specify pixels in double in the browser, and it really make difference when browser zoom is used.

Xizario
  • 481
  • 2
  • 9