
Microsoft Most Valuable Professional (MVP) Luc Van Dyck is webmaster of the Microsoft Business Solutions online community Mibuso.com. In addition to the site’s helpful discussion forums and downloads, mibuso.com features a collection of tips for Microsoft Dynamics NAV developers. This page is reprinted, with permission, from that collection. Sometimes you will want to execute a Microsoft Dynamics NAV object so that it behaves according to a new variable that you set. For example, suppose you have the standard Item List form that displays all items in the Microsoft Dynamics NAV database. Depending on where you call this form from, you may want some extra fields to be displayed and some other fields to be hidden. In this case, you need a way to pass a value or parameter to this Item List form so that you can hide some fields but make other fields visible. This tip shows you how to achieve those goals. In my previous developer tip, I showed you a way to pass parameters between Microsoft Dynamics NAV objects by writing functions that receive values. This tip explains how to perform the same task using a simpler approach: instead of writing a new function to pass a parameter, you’ll use the SingleInstance property to achieve this goal. In Microsoft Dynamics NAV version 3.01 and later, codeunits can have the SingleInstance property equal to Yes, as the following figure illustrates.  Figure 1: The SingleInstance property The Microsoft Dynamics NAV online Help describes this SingleInstance property as follows: When you set this property to Yes on codeunit X, all codeunit variables that use codeunit X will use the same instance. That is, all codeunit variables of codeunit X will use the same set of internal variables when the code is running on the same client. Codeunit X remains instantiated until you close the company. This means that the value of all variables used in a SingleInstance codeunit remains in memory—even after this codeunits exits. The second time you use a variable from the same codeunit, that variable will have the previous value instead of having the default value of 0 or appearing blank. To illustrate this, I’ll use the same two forms as I did in my previous tip, where you learned how to perform this task by writing your own functions. The next figure shows the first step in this process. In it, Form1 asks for a name and two decimal values.  Figure 2: A simple form After you click the Call Form2 button, these variables are passed to a SingleInstance codeunit. As you can see, Form2 retrieves the values from this codeunit, returns the result of the calculation to the codeunit, and displays the values.  Figure 3: The result of clicking the Call Form2 button When you click the Return to Form1 button, the focus returns to Form1. As the figure shows, Form1 retrieves the result of the calculation from the codeunit and displays the value.  Figure 4: The result of clicking the Return to Form1 button The big advantage of using this method is that you can transfer parameters without creating functions in every object. For example, say you have a form named Form1 that calls Form2. And suppose Form2 calls another form, Form3. If you’re using the function-writing method of passing parameters, when you want to pass a value from Form1 to Form3, you have to write a function on Form2 to retrieve the value from Form1, and you need a function on Form3 to retrieve the value from Form2. However, with the SingleInstance codeunit approach to passing parameters, Form1 sends the value to the codeunit, and Form3 retrieves the value from the codeunit. There is no need to modify Form2. Important: If the SingleInstance codeunit has already been executed and you want to modify the code inside the codeunit, you need to restart Microsoft Dynamics NAV. Otherwise, the version of the code that is already in memory will be executed instead of the new, modified version. Following is the code for the Call Form2 button of Form1, which passes the values to the codeunit and retrieves the result of the calculation: OnPush() cduPassParam.fctSetName(txtName); cduPassParam.fctSetDecimal1(decNumber1); cduPassParam.fctSetDecimal2(decNumber2); decResult := 0; IF FORM.RUNMODAL(50001) = ACTION::Close THEN decResult := cduPassParam.fctGetResult; Here is the code in the OnOpenForm trigger of Form2, which retrieves the values from the codeunit and sends back the result of the calculation: Form - OnOpenForm() txtName := cduPassParam.fctGetname; decNumber1 := cduPassParam.fctGetDecimal1; decNumber2 := cduPassParam.fctGetDecimal2; cduPassParam.fctSetResult(decNumber1 + decNumber2); And finally, here are the functions in the SingleInstance codeunit: fctSetName(ptxtName : Text[30]) txtName := ptxtName;
fctSetDecimal1(pdecNumber1 : Decimal) decNumber1 := pdecNumber1;
fctSetDecimal2(pdecNumber2 : Decimal) decNumber2 := pdecNumber2;
fctSetResult(pdecResult : Decimal) decResult := pdecResult;
fctGetname() : Text[30] EXIT(txtName);
fctGetDecimal1() : Decimal EXIT(decNumber1);
fctGetDecimal2() : Decimal EXIT(decNumber2);
fctGetResult() : Decimal EXIT(decResult); © Luc Van Dyck and mibuso.com. February 2005. Luc Van Dyck is a Microsoft Dynamics NAV Most Valuable Professional (MVP). He works as a software consultant forMindstone NV in Belgium. He is also the webmaster ofMibuso.com, which provides a platform for users and developers of products from the Microsoft Dynamics family to exchange ideas, tools, and how-to's, and to find business partners and products.
|