"as Number" vs. "as DecimalType"

If you look at the class hierarchy it looks something like this:

    Object
   /      \
   |      |
Number  State
   |      |
  DecimalType

NOTE: the above is notional, I think there is more stuff between State and Object

So what you get with DecimalType is an object that is both a Number and a State but when you use as Number you only have an object that is a Number.

Is there a best practice? I don’t know. I personally would recommend using as Number unless you know for sure you need to do something to it that requires it to be a state. There is one weird case where if one calls MyNumberItem.sendCommand(MyDecialType) where you will get an ambiguous method call exception because there is both a sendCommand(Number) and a sendCommand(DecimalType) and it doesn’t know which one you mean. But if you use as Number that error will go away.

1 Like