Using Codeunits as Classes

This time, I’ll present you with another way to use Codeunits in NAV.

By using Codeunits as parameters to a function, you can use Codeunits as (sort of .NET) Classes.
If you declare different local variables to the same Codeunit, they even have their own version.

For the sake of the example, I’ll ignore temporary data / multi user data.
Let’s assume we have a Buffer Record, which have multiple records with a type.

Something like this:

Name Datatype
Entry No. Integer
Parent Entry No. Integer
Type Option: ” ,Row,Column”
RowPropertyX Integer
RowPropertyY Boolean
ColumnPropertyX Text

To fill this table, we could off course, just put the data into the table directly. But that is not what this is all about. You could have a lot off fields with ambiguous names.
Another possibility is to make a “Helper” Codeunit. With functions:
– CreateHeader
– CreateRow(PropertyX,PropertyY)
– CreateColumn(PropertyX)

This still requires the user to know how the table must be set up, let alone when you add properties, you need to compile your code and check where you have compilation errors for the new parameters?

Another interesting way is creating multiple Codeunits like this:
– Header Object
– Row Object
– Column Object

We always start with a header object, and initialize it. On the Header Object, we only have properties / functions which are applicable to the header, and so on.

Here is a sample setup (this is not an NAV export, so you will not be able to import it)

We have a Header Object, which we can initialize and add rows.

In the row, we have again an initialize function and only properties and methods which interest us.

The Column would be something like this

The initialize function will work as needed for the specific Codeunit, but we will only manually access the initialize function of our header object.

Now imagine this:

First, we need to initialize the header. Next we have a header control, where the only option is to add a row. We get a Row Object returned.

On this Row object, we set the Property X to 12345.
We add another Row, but in a separate Row Object Codeunit.
Again we set the property X, this time to 54321

We add a column to both Rows and set the PropertyX to ‘abc’ and ‘def’.
This would be the actual result in the table:
– Entry No.: 1, Type: Blank
– Entry No.: 2, Parent Entry: 1, Type: Row, RowPropertyX: 12345
– Entry No.: 3, Parent Entry: 1, Type: Row, RowPropertyX: 54321
– Entry No.: 4, Parent Entry: 2, Type: Column, ColumnPropertyX: abc
– Entry No.: 5, Parent Entry: 3, Type: Column, ColumnPropertyX: def

Now, if we run the last 2 statements, you will see what I mean with “having their own instance”:
We set the column PropertyX to xyz, however, this is the last instance it was assigned, this is the result:
– Entry 5 will have a new ColumnPropertyX: xyz
– Entry 4 will still have ColumnPropertyX: abc

For the last statement, we explicitly change Row1, this is the result:
– Entry 2 will have a new RowPropertyX: 99999
– Entry 3 will still have RowPropertyX: 54321

Conclusion

Well, this is a small example of how you could use Codeunits as instantiated classes, and use them as parameters in a function. The initialize makes sure we “reset” all variables to what they need to be for your purpose.

It might not be the most “beautiful” example, but I do hope it serves as a clear example of the output.

P.S.

By the way, did you know that you could also use WITH statements with Codeunits?

P.S. 2

To save you some time, here is the sample code to import.

SampleCodeunitAsClass
SampleCodeunitAsClass
SampleCodeunitAsClass.txt
5.2 KiB
665 Downloads
Details...

One thought on “Using Codeunits as Classes

  1. We use this approach to have lists and dictionaries all around…

    PROCEDURE GetFormats@1000000006(pAlarmId@1000000000 : Integer;VAR pcList@1000000002 : Codeunit 50086);

    pcList.Clear;
    pcList.Add()
    pcList.Count()
    pcList.Delete()
    pcList.Find()

    and so on, very handy…

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.