Dynamic Dispatch In Navision

Achieving the impossible

Well, not quite, but Microsoft MBS Dynamics NAV (formerly Navision), has a built-in language called C/AL. This language is by no means very powerful nor very dynamic. In fact it’s very un-dynamic or static.

This simplicity comes at a price but it’s a two sided-sword in more than one way. The simplicity also adds monotonicity to the code, meaning all code approaches a similar well-recognizable form. Therein lies a great strength, and also a weakness. Often the power is not needed, but when it is, it is lacking.

One example of how to achieve a degree of polymorphism and dynamism in Navision is to use the variant type as a loophole into the world of polymorphism, or, more specifically dynamic dispatch.

Let’s have a concrete example

TypeName(var : Variant) : Text[30]
{
CASE TypeId(“var”) OF
0: EXIT(‘unknown’);
1: EXIT(‘boolean’);
2: EXIT(‘integer’);
3: EXIT(‘decimal’);
4: EXIT(‘date’);
5: EXIT(‘time’);
6: EXIT(‘char’);
7: EXIT(‘code’);
8: EXIT(‘text’);
9: EXIT(‘file’);
10: EXIT(‘action’);
11: EXIT(‘codeunit’);
12: EXIT(‘automation’);
13: EXIT(‘option’);
14: EXIT(‘binary’);
15: EXIT(‘dateformula’);
16: EXIT(‘transactiontype’);
17: EXIT(‘instream’);
18: EXIT(‘outstream’);
19: EXIT(‘record’);
END;
}

TypeId(var : Variant) : Integer
{
IF “var”.ISBOOLEAN THEN EXIT(1);
IF “var”.ISINTEGER THEN EXIT(2);
IF “var”.ISDECIMAL THEN EXIT(3);
IF “var”.ISDATE THEN EXIT(4);
IF “var”.ISTIME THEN EXIT(5);
IF “var”.ISCHAR THEN EXIT(6);
IF “var”.ISCODE THEN EXIT(7);
IF “var”.ISTEXT THEN EXIT(8);
IF “var”.ISFILE THEN EXIT(9);
IF “var”.ISACTION THEN EXIT(10);
IF “var”.ISCODEUNIT THEN EXIT(11);
IF “var”.ISAUTOMATION THEN EXIT(12);
IF “var”.ISOPTION THEN EXIT(13);
IF “var”.ISBINARY THEN EXIT(14);
IF “var”.ISDATEFORMULA THEN EXIT(15);
IF “var”.ISTRANSACTIONTYPE THEN EXIT(16);
IF “var”.ISINSTREAM THEN EXIT(17);
IF “var”.ISOUTSTREAM THEN EXIT(18);
IF “var”.ISRECORD THEN EXIT(19);
EXIT(0);
}

These two functions show how to achieve some level of dynamism. Now let’s expand it further.

xs_boolean(x : Boolean) : Text[1024] { … }

xs_integer(x : Integer) : Text[1024] { … }

xs_decimal(x : Decimal) : Text[1024] { … }

xs_format(x : Variant) : Text[1024]
{
CASE TypeId(x) OF
1: EXIT(xs_boolean(x));
2: EXIT(xs_integer(x));
3: EXIT(xs_decimal(x));
END;
}

That should show some of the possibilities of the variant type in Navision.

Note that it isn’t possible to return a variant.

Advertisements

About xosfaere

Software Developer
This entry was posted in Navision and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s