Variables
Scope: managed applications, mobile applications, and ordinary applications.
1. In general, it is not recommended that you use module variables. Instead, employ the appropriate development tools provided by 1C:Enterprise. Since the scope of such variables is difficult to control, they often become a source of hard-to-reproduce errors.
Examples of incorrect usage and exceptions to this rule are provided below. For guidelines on formatting variables in the module code, see Module structure.
2. Using variables in object modules (catalogs, documents, record sets, data processors, reports, etc.).
2.1. To pass parameters from external code to object module event handlers and between event subscription handlers, use the AdditionalProperties object property. Incorrect:
Var FileConversion Export;
Procedure BeforeWrite(Cancel)
If FileConversion Then
...
EndProcedure
// The calling code.
FileObject.FileConversion = True;
FileObject.Write();
Correct:
Procedure BeforeWrite(Cancel)
If AdditionalProperties.Property("FileConversion") Then
...
EndProcedure
// The calling code.
FileObject.AdditionalProperties.Insert("FileConversion", True);
FileObject.Write();
At the same time, it is a good practice to employ non-export object module variables, which are inaccessible from external code, for passing internal parameters between object module event handlers.
Example:
Var PreviousCompanyValue; // Value of the Company attribute before writing the object to the database.
Procedure BeforeWrite(Cancel)
PreviousCompanyValue = ...; // Get the value using a query before writing the object to the database.
EndProcedure
Procedure OnWrite(Cancel)
If PreviousAttributeValue <> Company Then
// Process the attribute value change during the writing.
...
EndIf;
EndProcedure
2.2. To handle return codes (errors) in the module logic, use string constants.
Incorrect:
Var NoErrors,
Error_FillCheckProcessing, // Occurs if the fill check data processor returns Cancel.
Error_WriteObject, // Occurs if an exception is thrown when writing an object.
Error_LockObject, // Occurs during an attempt to lock an object.
Procedure Recalculate()
...
Result = ProcessDocuments(...);
If Result = Error_WriteObject Then
...
ElsIf Result = Error_LockObject Then
...
ElsIf ...
EndProcedure
...
NoErrors = 1;
Error_FillCheckProcessing = 2;
Error_WriteObject = 3;
Error_LockObject = 4;
Correct:
Procedure Recalculate()
...
Result = ProcessDocuments(...);
If Result = "ErrorWriteObject" Then
...
ElsIf Result = "ErrorLockObject" Then
...
ElsIf ...
EndProcedure
2.3. To cache values that require long calculation time and are frequently used in procedures or functions, use memoization modules during server calls.
This rule does not apply to scenarios where returning a value calculated in an export function is not allowed due to security reasons. Use local module variables for storing such values.
3. Using variables in form modules.
3.1. To cache values that require long calculation time and are frequently used in procedures or functions, use memoization modules.
Do not cache static or quickly calculable data. In particular, do not cache values of predefined items or enumerations in client variables of form modules. Instead, use the PredefinedValue method to get them on the client.
3.2. To store intermediate calculation results and to pass them between form procedures and functions:
- Use parameters of procedures and functions to pass results through the call stack within a single procedure or function call context.
- Use form attributes to store intermediate results between different calls from the client. Note that values of server-side form module variables are not retained between calls from the client.
This rule does not apply to scenarios where client form variables are utilized to store intermediate results of form idle handlers, external event handlers, or client form item event handlers.
Example:
&AtClient
Var ImageSequenceNumber; // The counter used for naming files while scanning multiple images.
...
&AtClient
Procedure ExternalEvent(Source, Event, Data)
If Source = "TWAIN" AND Event = "ImageAcquired" Then
If ImageSequenceNumber = Undefined Then
ImageSequenceNumber = 1;
EndIf;
ImageSequenceNumber = ImageSequenceNumber + 1;
// Save the scanned document to a file with the ImageSequenceNumber number.
// ...
EndIf;
EndProcedure
4. For managed and ordinary applications, use variables to store client session parameters. For more information, see Session parameters.