Skip to content

Using a Property as a Constant

Wikipedia says:

In computer programming, a constant is a value that cannot be altered by the program during normal execution, i.e., the value is constant.

In Xojo we have constants that can be defined in code or added to modules, classes, etc. What you’ll notice about Wikipedia’s definition of “constant” is that it’s a behavior, not a specific type.

There is another way to define a “constant” or “a value that doesn’t change throughout the run of your application”.

A computed property with only the Get method defined that returns the same value every time is also “constant” in that it won’t vary as your program runs. It just happens to be labelled as a “property” and is also referred to as a “read-only” property. But the reality is if you set up a computed property that returns the same value every time you essentially have a “computed constant”.

Consider using one the next time you need something like a String or Text that is a series of Unicode code points since you can’t define one in the Property or Constant Editors without a bit of work (i.e. you can’t just type in &u21BE or a bunch of unicode code points one after the other).

If there are other values that you need to compute at runtime but remain consistent throughout a run of your application this is a decent way to make that happen.

There are some downsides because this is not a constant as far as the language is concerned. You cannot use it in these ways:

  • as a compile time check using #if
  • as part of another constant expression
  • as a default parameter value
  • as a default property value
  • as a declare library name

There may be other situations as well.

If you truly need an actual constant type, the alternative is to go to and copy and paste the symbols into the Constant Editor. This is a little more work but lets you define a true constant that is a bunch of Unicode code points.