Option Type Parameter Values

NAV Supports various data types for parameters to functions, one of which is an Option. An option data type is stored internally as a number but will appear on screen, in reports as text. In other systems this is referred to as an enumerated type.

Option type fields make it easy to write self-explanatory code, for example:

"Purchase Header".Type := "Purchase Header".Type::Invoice;

is a lot easier to understand than

"Purchase Header".Type := 2;

Easier to understand means less errors in the long run.

You’ve got to be careful where you use these types though. On a table field they’re great, but if you find yourself using them in a parameter to a function, take care. The system will only ensure that you are using an option type in the right place but will not check that the values in your options match. Here’s an example bit of code where the OptionString property for l_MyOptions is “Fish,Bandit,Notebook,Cheese” (don’t ask why those were the first words that sprang to mind).

I I have this code in my OnRun trigger of a codeunit…

l_MyOptions := l_MyOptions::Fish;

MESSAGE('l_MyOptions is %1.', l_MyOptions);
TestFunc(l_MyOptions);

and then create a function that takes an option parameter…

TestFunc(p_Option : 'Wombat,Dog,Tasmanian Devil,Kiwi')
MESSAGE('p_Option is %1.', p_Option);

My output looks something like this…

 image

image

The consequences of this are if I decide to add a new option to my parameter on my function, I need to search for every instance where I call this function to make sure I am using the same option string there. I know this because I’ve just had to do that in a modification and this is time that could have been better spent playing addictive games on Facebook.

So what are the solutions? Well one way around this would be to use a field on a table to pass these values across. You can’t use table fields as parameters, but you can use records, so you could pass the entire record. This is good because a change to the option type on the table means the new options are automatically available everywhere.

What would be even better, would be if we had true Extended Data Types in C/AL. The extended data type would be declare as a new object – a bit like a table. We could then use these extended data types in fields and forms. This is one feature from AX that I really miss in NAV. The advantage is that when I need to change my data type (increasing the size of an address field for example) I just change the EDT and the change gets reflected everywhere that EDT is used. Now that would really be something.


Related
Recommended