Clear & ClearAll Single Instace Codeunit #305
Replies: 3 comments
-
Good point and thank you for raising a discussion here. If I understand correctly these are the both cases you're referring to? codeunit 50100 MyCodeunit
{
procedure DoSomething()
var
MySingleInstanceCodeunit: Codeunit MySingleInstanceCodeunit;
begin
// Raise a LC00xx rule here
Clear(MySingleInstanceCodeunit);
end;
}
codeunit 50101 MySingleInstanceCodeunit
{
SingleInstance = true;
procedure ClearAllTheVariables()
begin
// Raise a LC00xx rule here
ClearAll();
end;
} Do we need to verify if there are any global variables defined in the codeunit, or always raise the rule? |
Beta Was this translation helpful? Give feedback.
-
Did some more testing, and it's a bit more complicated it seems. codeunit 50100 MyCodeunit
{
var
MySingleInstanceCodeunit: Codeunit MySingleInstanceCodeunit;
MyDocumentNumber: Code[20];
procedure DoSomething()
begin
MyDocumentNumber := 'SomeRandomValue';
MySingleInstanceCodeunit.Set(MyDocumentNumber);
Message('1. Value in the MyDocumentNumber is ''%1''.', MyDocumentNumber);
Message('2. Value in the MySingleInstanceCodeunit is ''%1''.', MySingleInstanceCodeunit.Get());
ClearAll();
Message('3. Value in the MyDocumentNumber after a ClearAll() is now ''%1''.', MyDocumentNumber);
Message('4. Value in the MySingleInstanceCodeunit after a ClearAll() is now ''%1''.', MySingleInstanceCodeunit.Get());
Clear(MySingleInstanceCodeunit);
Message('5. Value in the MySingleInstanceCodeunit after a Clear() is now ''%1''.', MySingleInstanceCodeunit.Get());
MySingleInstanceCodeunit.ClearAllTheVariables();
Message('6. Value in the MySingleInstanceCodeunit after a executing the ClearAllTheVariables() is now ''%1''.', MySingleInstanceCodeunit.Get());
end;
}
codeunit 50101 MySingleInstanceCodeunit
{
SingleInstance = true;
var
GlobalDocumentNumber: Code[20];
procedure Set(NewDocumentNumber: Code[20])
begin
GlobalDocumentNumber := NewDocumentNumber;
end;
procedure Get(): Code[20]
begin
exit(GlobalDocumentNumber);
end;
procedure ClearAllTheVariables()
begin
ClearAll();
end;
}
So in conclusion Doubting on what to do with the global variable of the type record inside the referencing codeunits? And do we have a "inception situation" going on here? The referencing single instance codeunit has a global variable to another single instance codeunit and so on. This could lead to a point that the rule could get confusing? codeunit 50100 MyCodeunit
{
var
MySingleInstanceCodeunit: Codeunit MySingleInstanceCodeunit;
MySingleInstanceCodeunitWithOnlyGlobalRecords: Codeunit MySingleInstanceCodeunitWithOnlyGlobalRecords;
procedure DoSomething()
begin
// Raise a LC00xx rule here (MySingleInstanceCodeunit is Single Instance ánd has non-Record type Global variables)
Clear(MySingleInstanceCodeunit);
// Do not raise a LC00xx rule here (MySingleInstanceCodeunitWithOnlyGlobalRecords is Single Instance, but doesn't have non-Record type Global variables)
Clear(MySingleInstanceCodeunitWithOnlyGlobalRecords);
// Raise a LC00xx rule here (MySingleInstanceCodeunit is Single Instance ánd has non-Record type Global variables)
ClearAll();
end;
}
codeunit 50101 MySecondCodeunit
{
var
MySingleInstanceCodeunitWithOnlyGlobalRecords: Codeunit MySingleInstanceCodeunitWithOnlyGlobalRecords;
procedure DoSomething()
begin
// Do not raise a LC00xx rule here (MySingleInstanceCodeunitWithOnlyGlobalRecords is Single Instance, but doesn't have non-Record type Global variables)
Clear(MySingleInstanceCodeunitWithOnlyGlobalRecords);
// Do not raise a LC00xx rule here (MySingleInstanceCodeunitWithOnlyGlobalRecords is Single Instance, but doesn't have non-Record type Global variables)
ClearAll();
end;
}
codeunit 50102 MyThirthCodeunit
{
var
MySingleInstanceCodeunitWithAnotherSingleInstanceCodeunit: Codeunit MySingleInstanceCodeunitWithAnotherSingleInstanceCodeunit;
procedure DoSomething()
begin
// Raise a LC00xx rule here (In the reference tree a reference to the MySingleInstanceCodeunit)
Clear(MySingleInstanceCodeunitWithAnotherSingleInstanceCodeunit);
// Raise a LC00xx rule here (In the reference tree a reference to the MySingleInstanceCodeunit)
ClearAll();
end;
}
codeunit 50103 MySingleInstanceCodeunit
{
SingleInstance = true;
var
GlobalCustomerRec: Record Customer;
GlobalDocumentNumber: Code[20];
procedure Set(NewDocumentNumber: Code[20])
begin
GlobalDocumentNumber := NewDocumentNumber;
end;
procedure Set(NewCustomerRec: Record Customer)
begin
GlobalCustomerRec := NewCustomerRec;
end;
procedure Get(): Code[20]
begin
exit(GlobalDocumentNumber);
end;
procedure ClearAllTheVariables()
begin
// Do not raise a LC00xx rule here
ClearAll();
end;
}
codeunit 50104 MySingleInstanceCodeunitWithOnlyGlobalRecords
{
SingleInstance = true;
var
GlobalCustomerRec: Record Customer;
procedure Set(NewCustomerRec: Record Customer)
begin
GlobalCustomerRec := NewCustomerRec;
end;
procedure Get() Customer: Record Customer;
begin
exit(GlobalCustomerRec);
end;
procedure ClearAllTheVariables()
begin
// Do not raise a LC00xx rule here
ClearAll();
end;
}
codeunit 50105 MySingleInstanceCodeunitWithAnotherSingleInstanceCodeunit
{
SingleInstance = true;
var
MySingleInstanceCodeunit: Codeunit MySingleInstanceCodeunit;
} |
Beta Was this translation helpful? Give feedback.
-
I've created an rule for this, and available in the prelease: #348 |
Beta Was this translation helpful? Give feedback.
-
Since
Clear(SingleInstanceCodeunit)
; orClearAll();
in a single Instance codeunit does not work as expected (does not clear global variables) LinterCop should show a warning.Beta Was this translation helpful? Give feedback.
All reactions