How to multiply a Unit by a number?
The
Unit
type exists to represent a length, and contains two
different informations: a value and a unit or type. The unit
can be absolute (pixels, centimeters, etc.) or relative
(percentage, em). They are not lengths in the physical sense
because in the relative case, they vary depending on the
context. This is just how HTML works with lengths.
So every property that will end up representing a length
should be of type
Unit.
Now, while
Unit
is a value type (a struct, if you prefer, as opposed to a
class), it lacks the basic arithmetic operators. You can't
add two
Units, but that's normal, because they may have different units
(again, they are not lengths in the physical sense), but you
can't multiply them by a number either without writing some
code.
An example where you would want to do that is for example
the Menu control where we have the possibility to indent the
submenus. A menu of depth three will have to be indented by
three times the indent property value.
We're doing it more or less this way:
Unit Multiply(Unit indent, int depth) {
if (indent.IsEmpty) {
return Unit.Empty;
}
if (indent.Value == 0) {
return indent;
}
double
indentValue = indent.Value * depth;
// I'd love not to hardcode this but Unit.MaxValue is
currently private.
if
(indentValue < 32767) {
return new Unit(indentValue, indent.Type);
}
else
{
return new Unit(32767, indent.Type);
}
}