-
Notifications
You must be signed in to change notification settings - Fork 33
LC0032
Arthur edited this page Nov 20, 2023
·
4 revisions
Using Clear()
on a single instance codeunit variable does not affect the global variables for these codeunits and could lead to unexpected results. This also applies to ClearAll()
in a object (page, codeunit, ..) where local or global single instance codeunit variables are defined.
codeunit 50100 MyCodeunit
{
procedure DoSomething()
var
MySingleInstanceCodeunit: Codeunit MySingleInstanceCodeunit;
begin
Clear(MySingleInstanceCodeunit); // This will not(!) clear the GlobalDocumentNumber variable in the MySingleInstanceCodeunit
end;
}
codeunit 50101 MySingleInstanceCodeunit
{
SingleInstance = true;
var
GlobalDocumentNumber: Code[20];
}
codeunit 50100 MyCodeunit
{
var
MySingleInstanceCodeunit: Codeunit MySingleInstanceCodeunit;
procedure DoSomething()
begin
ClearAll(); // This will not(!) clear the GlobalDocumentNumber variable in the MySingleInstanceCodeunit
end;
}
codeunit 50101 MySingleInstanceCodeunit
{
SingleInstance = true;
var
GlobalDocumentNumber: Code[20];
}
Both functions, Clear() and ClearAll(), do not clear variables of the type Record. So this rule will ignore these types of variable.
codeunit 50100 MyCodeunit
{
var
MySingleInstanceCodeunitWithOnlyGlobalRecords: Codeunit MySingleInstanceCodeunitWithOnlyGlobalRecords;
procedure DoSomething()
begin
// Do not raise a LC0032 rule here (MySingleInstanceCodeunitWithOnlyGlobalRecords is Single Instance, but doesn't have non-Record type Global variables)
Clear(MySingleInstanceCodeunitWithOnlyGlobalRecords);
// Do not raise a LC0032 rule here (MySingleInstanceCodeunitWithOnlyGlobalRecords is Single Instance, but doesn't have non-Record type Global variables)
ClearAll();
end;
}
codeunit 50101 MySingleInstanceCodeunitWithOnlyGlobalRecords
{
SingleInstance = true;
var
GlobalCustomerRec: Record Customer;
}
There's also an exception though for when the single instance codeunit, that is found, is the same as the codeunit itself.
codeunit 50100 "My Single Instance Codeunit"
{
EventSubscriberInstance = Manual;
SingleInstance = true;
var
MySingleInstanceCodeunit: Codeunit "My Single Instance Codeunit";
myBoolean: Boolean;
myDecimal: Decimal;
#region ClearEverythingThatIsStored
internal procedure ClearEverythingThatIsStored()
begin
// Do not raise a LC0032 rule here (MySingleInstanceCodeunit is the same object as itself)
ClearAll();
end;
#endregion ClearEverythingThatIsStored
#region DoSomething
internal procedure DoSomething(): Decimal;
begin
// Just some code
if BindSubscription(MySingleInstanceCodeunit) then
if myBoolean then
exit(myDecimal);
end;
#endregion DoSomething
// EventSubscriber that is only active when this codeunit is Bound.