Codeunit

what is Single Instance Codeunit in Navision and how can use this?

thanks

Sunil Mishra

Searching for Single Instance in the NAV C/SIDE Help will give you this:

Single Instance Codeunit

In some cases, only one instance of a codeunit needs to exist. This means that all the codeunit variables of a particular codeunit use the same set of variables. When you set the SingleInstance property of the codeunit to Yes, all the codeunit variables of that codeunit use the same instance, thereby allowing you to create global variables.

Note


We recommended that you avoid using global variables for most types of code. However, in certain situations, it may be necessary to use them, for example, to ensure that you are only using one instance of an external variable.

A single instance codeunit is instantiated when you use it for the first time. Normal codeunit instances (codeunits that do not have the SingleInstance property set) are deleted when the last codeunit variable that uses that codeunit instance goes out of scope. However, single instance codeunits remain instantiated until you close the company.

For an example of a single instance codeunit, see Walkthrough: Using Codeunits.

If you are familiar with static variables and functions from object oriented programming is basically the same thing.

Hi Sunil,

A single instance codeunit is a codeunit that will stay memory resident once call. An example would be a polling codeunit for a serial port.

The following example shows how you can use the SingleInstance property.

Two forms can connect to the same codeunit.

On Form1:

Codeunit1.SetNumber(100);

On Form2:

Number := Codeunit1.GetNumber(); MESSAGE(Format(Number));

The SingleInstance property in Codeunit1 is set to Yes. Form1 calls a function on Codeunit1 and sets the parameter to 100. Codeunit1 saves this parameter in a local variable. Form2 is now able to get the parameter value (=100) from Codeunit1. A message is displayed.

Thanks

Jerome Marshal.J