Single Instance CodeUnit - limitation?

Hi. I have a single instance code unit to make use of “global variables”. It contains a Code type variable and many Boolean type variables. Each one uses the logic below (for Code type variable) to set and get the variables. SetDefaultSite(InputValue : Code[20]) GlobalDefaultSite := InputValue; GetDefaultSite() OutputValue : Code[20] OutputValue := GlobalDefaultSite; where GlobalDefaultSite is declared as Code 20. My problem is whenever I try to add another Code type variable I can not access it (I successfully created a new CodeUnit and it worked, but again when I tried to add a second Code type variable to this new CodeUnit it also failed). Using the example above (if this was the second declaration of the Code type field)… SetDefaultSite(‘XXX’); errors with “Reference to the member SetDefaultSite of the variable could not be solved” (double space between variable and could as if it has blanked out the variable name). Ideally I would prefer NOT to have to create a new CodeUnit for each variable I want to store. Any help appreciated. Regards, Gareth.

As far as I know there isn’t any limitations on how many Code type variables you declare in ota Codeunit. You cannot access it … it means that you can’t declare it or you can’t use it in code during runtime?

I can declare the variable and compile the CodeUnit no problem. It is when another object goes to run the Set (or Get) function for the second Code type variable that the error described happens. Below is an example that errors for me (3.60) of the codeunit and a simple form - note on the form that the first variable Set’s and Get’s ok but the second Set errors. Regards, Gareth. OBJECT Form 59977 GV Error { OBJECT-PROPERTIES { Date=15/04/03; Time=13:45:29; Modified=Yes; Version List=; } PROPERTIES { Width=8000; Height=8000; OnOpenForm=BEGIN gv2.SetDefaultSite(‘HELLO’); MESSAGE(gv2.GetDefaultSite); gv2.SetSwipeCardVehicle(‘GOODBYE’); END; } CONTROLS { } CODE { VAR gv2@1000000000 : Codeunit 50006; BEGIN END. } } OBJECT Codeunit 50006 Global Variables 2 { OBJECT-PROPERTIES { Date=15/04/03; Time=12:55:57; Modified=Yes; Version List=; } PROPERTIES { SingleInstance=Yes; OnRun=BEGIN END; } CODE { VAR GlobalDefaultSite@1000000000 : Code[20]; GlobalSwipeCardVehicle@1000000001 : Code[20]; PROCEDURE SetDefaultSite@1000000001(InputValue@1000000000 : Code[20]); BEGIN GlobalDefaultSite := InputValue; END; PROCEDURE GetDefaultSite@1000000002() OutputValue : Code[20]; BEGIN OutputValue := GlobalDefaultSite; END; PROCEDURE SetSwipeCardVehicle@1000000000(InputValue@1000000000 : Code[20]); BEGIN GlobalSwipeCardVehicle := InputValue; END; PROCEDURE GetSwipeCardVehicle@1000000003() OutputValue : Code[20]; BEGIN OutputValue := GlobalSwipeCardVehicle; END; BEGIN { This is a single instance codeunit to store information such as the current swipe card User ID } END. } }

I can’t imagine a solution but why do you want to use seperate Codeunit for saving these variables not current form or table?

I need these variables kept for the whole login session, so that when other forms are loaded they know the values of these variables. Each form loaded then reads the variable from this central single instance code unit. Another alternative (which requires more disk activity) is to have all these variables as fields in a table which is keyed on Session.“Connection ID”. I just thought the use of a single instance variable would be more “tidy” than writing to tables. Regards, Gareth.

We solved this problem by saving these values un User table. In this case there can’t be conflicts between users and no new codeunits needed. I don’t think this takes soooo much disk activity [;)]

If you make changes to a single instance codeunit during a session after the codeunit has been run (i.e. the single instance of the codeunit has been created) then those changes will not be available until you take some action to clear the single instance codeunit from memory (e.g. change the company).

There’s a limit on the number of global variables you can have in a function in navision… it’s giving a lot of troubles recognizing them for other objects… (i know because i experienced that problem in a couple times). If your problem are just boolean variables i should recommend you a different approach: 1) Create a unique variable type boolelan and set on it dimensions (make it being an array). 2) Create a function where you can recognize the “parameter” string you’re wanting to handle for returning the right value from the array. 3) Create a funcetion also for handling setting the values. Example: VAR mybooleanarray : boolean [1024] function getvalue ( whatvariable : string[30]) : boolean; BEGIN CASE whatvariable OF ‘userconnect’ : EXIT(getbooleanvalue(2)); ‘site’ : EXIT(getbooleanvalue(2)); ELSE ERROR(Text50000); END; END; Function getbooleanvalue (whatvalue: integer) :boolean; BEGIN IF (whatvalue <0) THEN ERROR (Text50001); IF (whatvalue >= 1024) THEN ERROR (Text50002); EXIT (mybooleanarray[whatvalue]); END; …

BTW… the limit is also affecting local variables in a function… so if you’re having a function with a lot of local variables sometimes you need creating another function and moving part of those variables and processing to that new function in order to make navision to work fine… I suppose that you’re also having a limit on the number of functions you can have… but i had never have that other problem. Regards,

Can’t you just create new Table to save your values not Codeunit?

Thanks to everyone for your ideas. I’ve gone with the codeunit with one Code type variable with dimensions - this appears to work great. I’ve tried to stay away from the writing to tables, in case I need to use these variables when I’m half way through a transaction that I don’t want to commit (but will need to, to say run a form modally). Thanks again, Gareth.