Dynamics 365 Business Central: How to get the next or previous letter in the alphabet

Dynamics 365 Business Central

Hi, Readers.
I recently received a question, is there any way to get the next or previous letter in the alphabet in AL.
For example, if the input was equal to ‘c’, they want to output ‘b’ and ‘d’.

In this post, I would like to share two easy way about this, hope it will help.

1. Using ASCII

As you might know, we can do this easily by using ASCII in SQL Query.

PS: ASCII Code for Letters

LetterASCII CodeLetterASCII Code
a97A65
b98B66
c99C67
d100D68
e101E69
f102F70
g103G71
h104H72
i105I73
j106J74
k107K75
l108L76
m109M77
n110N78
o111O79
p112P80
q113Q81
r114R82
s115S83
t116T84
u117U85
v118V86
w119W87
x120X88
y121Y89
z122Z90

Can we do the same thing in AL?
Yes, we can use the CHAR Data Type to get the ordinal value.
Let’s see two examples:
1. From a letter to get ASCII Code.

2. From ASCII Code to get a letter.

Okay, now you can easily get the next or previous letter. (Please note that you may also get symbol)

Test Video:

Source Code:

page 50100 "Get next or previous letter"
{
    PageType = Card;
    ApplicationArea = All;
    UsageCategory = Administration;

    layout
    {
        area(Content)
        {
            group(Input)
            {
                field(InputLetter; InputLetter)
                {
                    Caption = 'Input Letter';
                    ApplicationArea = All;

                    trigger OnValidate()
                    var
                        InputLetterASCII: Integer;
                    begin
                        InputLetterASCII := InputLetter;
                        PerviousLetter := ' ';
                        NextLetter := ' ';
                        PerviousLetter := InputLetterASCII - 1;
                        Nextletter := InputLetterASCII + 1;
                    end;

                }
            }
            group(Result)
            {
                field(PerviousLetter; PerviousLetter)
                {
                    Caption = 'Pervious Letter';
                    ApplicationArea = All;
                    Editable = false;
                }
                field(NextLetter; NextLetter)
                {
                    Caption = 'Next Letter';
                    ApplicationArea = All;
                    Editable = false;
                }
            }
        }
    }

    var
        InputLetter: Char;
        NextLetter: Char;
        PerviousLetter: Char;
}
2. Using List Data Type

List Data Type: Represents a strongly typed list of ordered objects that can be accessed by index. Contrary to the Array data type, a List is unbounded, such that its dimension does not need to be specified upon declaration.

So you can pre-store the letters to a List and then get the letter at the specified index.

For example:

Note: List.Get Method will raise an error if the index is outside the valid range.

Test Video:

Source Code:

page 50100 "Get next or previous letter"
{
    PageType = Card;
    ApplicationArea = All;
    UsageCategory = Administration;

    layout
    {
        area(Content)
        {
            group(Input)
            {
                field(InputLetter; InputLetter)
                {
                    Caption = 'Input Letter';
                    ApplicationArea = All;

                    trigger OnValidate()
                    var
                        Index: Integer;
                        AlphabetValueSet: Text;
                        AlphabetList: List of [Text];
                    begin
                        PerviousLetter := '';
                        NextLetter := '';
                        AlphabetValueSet := 'a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z';
                        AlphabetList := AlphabetValueSet.Split(' ');
                        if not AlphabetList.Contains(InputLetter) then
                            Error('Please enter a single letter');
                        Index := AlphabetList.IndexOf(InputLetter);
                        if InputLetter <> 'a' then
                            PerviousLetter := AlphabetList.Get(Index - 1);
                        if InputLetter <> 'Z' then
                            Nextletter := AlphabetList.Get(Index + 1);
                    end;

                }
            }
            group(Result)
            {
                field(PerviousLetter; PerviousLetter)
                {
                    Caption = 'Pervious Letter';
                    ApplicationArea = All;
                    Editable = false;
                }
                field(NextLetter; NextLetter)
                {
                    Caption = 'Next Letter';
                    ApplicationArea = All;
                    Editable = false;
                }
            }
        }
    }

    var
        InputLetter: Text;
        NextLetter: Text;
        PerviousLetter: Text;
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL