Skip to content
Arthur edited this page Feb 21, 2024 · 6 revisions

Zero (0) Enum value should be reserved for Empty Value.

This rule raises a diagnostic if an Enum value with Id = 0 has a Name or Caption that isn't an empty string (whitespace excluded).
If the Enum object implements an Interface, the rule is not applicable and wil not raise a diagnostic.

When we receive a request to add an empty value to an existing Enum and position it at the beginning, having reserved the zero (0) value for Empty values, we can easily handle such requests.

Zero vs Null

In the database Enums are stored as an Integer, where Business Central doesn't support nullable values.
A new record will automatically be assigned the zero (0) Enum value, and with a Table Extension all the existing records also. Relying on this implicit usage can be ambiguous whether this value was set intentional.

To avoid this skip the zero (0) Enum value and start with one (1) or higher for Enum values that aren't empty.

enum 50100 MyEnum
{
    value(1; Quote)
    {
        Caption = 'Quote';
    }
}

Whitespace for Empty Value

enum 50100 MyEnum
{
    value(0; " ") // This value can be selected
    {
        Caption = ' ', Locked = true;
    }
    value(0; "") // This value can not(!) be selected
    {
        Caption = '', Locked = true;
    }
}

Interfaces

This rule excludes Enum objects which implements an interface.

enum 50100 MyCustomCodeAnalyzer implements myInterface
{
    value(0; LinterCop) // Allow zero value with non-Empty Value
    {
        Caption = 'LinterCop';
    }
    value(1; CompanialCop)
    {
        Caption = 'CompanialCop';
    }
}

Pragma

#pragma warning disable LC0045 // Allow Zero (0) Enum for non-Empty Value
enum 50100 MyEnum
{
    value(0; Quote)
    {
        Caption = 'Quote';
    }
}
Clone this wiki locally