1c enterprise 8.2 periodic variables. Comparison with constant value in queries

Constants are used to store a single value in the database. Usually, constants store information that is rarely changed, for example, the name of the organization, the address of the organization, the name of the manager, etc.

For each constant, you must specify its name and data type.Accessing constants in a code module is very simple:

1. Reading the value of a constant

// read the value of the Constant
Name \u003d Constants. Name of the organization... Get ();

Note the word Constants (not Constant as it was in version 7.7).

2. Writing a new constant value

To write (set) a new constant value, use the Set () method

// write the new value of the Constant
Constants. Name of the organization... Install ("Horns and Hooves LLC");

3. Working with a set of constants

Sometimes it may be necessary to read or write the values \u200b\u200bof several constants at once. For such situations, you can use a set of constants. When using a set, all constants included in it are read and written together in one transaction, i.e. operation on several constants is performed simultaneously.

// create a set of three constants

// note that we are only creating a set of constants and do not know their value yet

Set \u003d Constants. CreateSet ("Head, Organization Name, Organization Address");

Since when working with a set of constants, we can change (write) the values \u200b\u200bof all constants of the set at the same time, we first need to set a new value for each constant, and then use the Write () method to write them simultaneously.

// set the value of the constants in the set (not in the base)
Set. Manager \u003d "Ivanov I.I.";
Set.NameOrganization \u003d "LLC New Horns and Hooves";
Dialing.AddressOrganization \u003d "Russia, Moscow, the Kremlin";
Set. Write (); // write the values \u200b\u200bof three constants to the base at once

4. Comparison with the value of a constant in queries

Sometimes we may need to compare data with the value of a constant in a query. In this example, we select records from the Reminders lookup, for which the value of the Code attribute is equal to the value of our constant OurNumber. We will consider the 1C query language in more detail in the following materials on the built-in 1C language.

"CHOOSE
| OurP.Value,
| Reminders Code,
| FROM
| Constant.Our Number AS Ours,
| Reference.Reminders AS Reminders
| WHERE
| Reminders.Code \u003d OurPervalue "

Comments

    // now using the Get () method we can read the values \u200b\u200bof the constants of our set

    Set.Read(); // read three constants from the database at once

    I wonder if there is a typo here? It seemed like it was about a methodGet().

Description:

Keyword A procedure begins a section of the source text, the execution of which can be initiated from anywhere in the program unit, simply by specifying Procedure_name with a list of parameters (if no parameters are passed, then parentheses are nevertheless required). If the Export keyword is used in the body of a procedure description in a regular application module, a managed application module, or a common program module, this means that this procedure is available from all other program configuration modules.

When the Return statement is executed, the procedure ends and returns control to the call point. If the text of the procedure does not contain the Return operator, then after the execution of the last executed operator, the implicit Return operator is executed. The end of the program section of the procedure is determined by the EndProcedure statement.

Variables declared in the body of a procedure in a section are local variables of this procedure, therefore, they are available only in this procedure (unless they are passed as parameters when calling other procedures, functions or methods).

Syntax:

Parameters:

ProcName Assigns the name of the procedure.Value An optional keyword that indicates that the parameter following it is passed by value, that is, changing the value of the formal parameter during the procedure execution will not affect the actual parameter passed during the procedure call. If this keyword is not specified, then the procedure parameter is passed by reference, that is, a change in the value of the formal parameter within the procedure will lead to a change in the value of the corresponding actual parameter.Param1, ..., ParamN Optional list of formal parameters separated by commas. The values \u200b\u200bof the formal parameters must match the values \u200b\u200bof the actual parameters passed when calling the procedure. This list defines the names of each of the parameters as they are used in the procedure text. The formal parameter list may be empty.Export An optional keyword that indicates that the procedure is available from other program modules.Local variables are declared and can only be referenced within this procedure.// Operators Executable procedure operators.Return An optional keyword that terminates the execution of the procedure and returns to the point in the program from which the procedure was called. The use of this operator in a procedure is optional.EndProcedure Mandatory keyword denoting the end of the procedure source text, the end of the procedure. Return to the point from which the procedure was called.

Function

Description:

Keyword Function begins a section of the source text of the function, the execution of which can be initiated from anywhere in the program unit by simply specifying Function_name with a list of parameters (if no parameters are passed, then parentheses are nevertheless required). If the Export keyword is used in the body of a function description in a module of a regular, managed application, external connection, session, or general program module, this means that this function is available from all other program modules of the configuration.

Functions differ from procedures only in that they return Return value... The end of the program section of a function is defined by the EndFunction statement.

A call to any function in the text of a program module can be written as a procedure call, that is, in the language it is allowed not to receive from a function return value.

Variables declared in the body of the function in the section Local variable declarations, are local variables of this function, therefore they are available only in this function (unless they are passed as parameters when calling other procedures, functions or methods).

Syntax:

Parameters:

Function_name Assigns the name of the function.Value An optional keyword that indicates that the next parameter is passed by value, i.e. changing the value of a formal parameter during function execution will not affect the actual parameter passed when the function is called. If this keyword is not specified, then the function parameter is passed by reference, that is, changing the value of the formal parameter inside the function will change the value of the corresponding actual parameter.Param1, ..., ParamN Optional list of formal parameters separated by commas. The values \u200b\u200bof the formal parameters must match the values \u200b\u200bof the actual parameters passed when the function is called. This list defines the names of each of the parameters as they are used in the function text. The formal parameter list may be empty.\u003d DefVal An optional setting of the parameter's default value. Parameters with their default values \u200b\u200bcan be placed anywhere in the formal parameter list.Export An optional keyword that indicates that this function is available from other software modules.// Local variable declarationsLocal variables are declared that can only be referenced within this function.// Operators Executable function operators.Return Return valueA keyword that ends the execution of a function and returns the specified value in the expression in which the function is used. The return value can be an expression or a variable, the value of which contains the result of calling the function.EndFunction Required keyword indicating the end of the function source.

As a rule, learning any programming language starts with an example of writing the first simplest program (“Hello world!”). This is done in order to visually show the work with basic syntactic constructions. We will not make an exception to this generally accepted way of presenting material when exploring a new development environment, and therefore our first article should be viewed in the same vein. In it, we will analyze in detail the answers to the following questions regarding programming on the 1C: Enterprise 8 platform:

  • Where and with what help to write the program code in the built-in 1C language?
  • What are software modules, what are the rules for working with them?
  • What is a variable, how to work with it, how and where to declare it?
  • What comparison, assignment, and conditional operators are there, and how do you apply them?
  • Boolean operations - what are they and how to work with them?
  • Why are loops and how do I use them?

The article will be useful to all those who are not yet familiar with the development on the 1C: Enterprise 8 platform, but want to learn how to program on 1C.

Applicability

The material is relevant for the 1C: Enterprise 8 platform, edition 8.2. and 8.3.

Variables and Operators

In this article, we begin to study the built-in language 1C: Enterprise 8. The executable code is contained in software modules.

There are a large number of modules that are designed to handle various events.

So, the user's login to the system is processed in one module, and the processing of the user's clicking on a certain button is processed in a completely different one.

Thus, each module describes the behavior of the configuration at a certain point. The module contains, first of all, a section describing variables. Those. we can declare some variables in the module.

In the future, they can be used in the procedures and functions of this module. If a variable is defined with the Export keyword, then it will be available outside of this module. An example of a variable declaration line:

Change Warehouse, Division, Storekeeper Export;

After the declaration of variables, there is a section of procedures and functions.

Behind them is the section of the main program, which will be executed at the time of access to this module.

For example, in the main program section, you can initialize variables, i.e. assign them some initial values:

State \u003d 1;
NewExpression \u003d 2;
Result \u003d 3;

A module can be thought of as a combination of different operators that perform different actions we need.

The operator separator is the ";" (semicolon). This character is the end of the statement. Those. the operator can be written as follows:

Result \u003d 100X200
+400
-600;

It doesn't matter how many lines the operator is on.

Of course, more often it is more convenient and clearer to place an operator in one line, but sometimes the operators are quite long (the number of lines can reasonably reach several dozen).

A semicolon can be omitted in the final statement of a given construction, for example, a procedure. Those. the following code will work:

Procedure CalculationValue ()

InitialValue \u003d 100;
IntermediateValue \u003d InitialValue / 5;
FinalValue \u003d InitialValue + IntermediateValue

End of Procedure

However, it is better to use a semicolon in the terminating statement. It is possible that over time, the construction will be continued, and the final statement will no longer be final. We'll have to specifically monitor this situation.

Variables are designed to store some values \u200b\u200bof any data type. They are used for intermediate storage of information for processing.

Almost any program module that performs some action has various variables. The typing of variables by values \u200b\u200bin Platform 1C: Enterprise 8 is soft.

For example, a variable can contain a value of one data type, and a few lines later - another type:

Created \u003d False;
Created \u003d True;
Created \u003d 100;

In the first two operators, the value of the variables is Boolean, and in the third it is changed to a numeric value. Those. typing depends on the value that is assigned to a given variable.
Variables can be described in two ways:

  • implicit way (the mention on the left side of the assignment operator describes this variable, there is no preliminary description of the variable with the word Var, i.e. there is no special section for describing variables);
  • explicit description of variables (Variable ControlData;). Explicit description of variables is used, for example, if the subsequent transfer of this variable to a function is expected.

For the names of variables, the classic description of the identifier is used. The identifier consists of letters, numbers and underscores. The identifier must begin with either a letter or an underscore.

In this case, the name of the variable should reflect the meaning of this variable. Single letter variable names (like A, B, C) are bad examples. They do not reflect the nature of the variables.

Examples of correct variable names: Counter (increasing variable for the loop), Counterparty. If a variable name contains several words, then each new word, for clarity, should start with a capital letter.

You cannot use reserved words in variable names such as Procedure, Function, Loop, End of Loop, etc. (these constructions are highlighted in red in the program module).

Reserved words are built-in language operators and there are quite a few of them. All of them are presented in Syntax Assistant.

It should be noted that data types do not apply to reserved words (for example, Array, Boolean, True, False). The system will accept such variable names correctly.

For writing program code, case does not matter. For example, the word Procedure can be written with both capital and small letters. Moreover, uppercase and lowercase letters can be alternated within a word.

It doesn't matter to the Platform. Nevertheless, according to the rules of good form, the beginning of a word should be written with a capital letter, all other letters in small letters.

About the language. You can use Russian, and English, and a combination of two languages. If it is convenient for someone, you can safely use English to write program code, as well as combine Russian and English. This is not important for the Platform.

Many names in English are difficult to remember. When using a combination of two languages, the readability of the program code deteriorates.

Boolean operations

Comparison operators often use Boolean logic that returns True or False.

For example, in the conditional operator you can compare: If Event \u003d Sale Then the algorithm will follow one branch (ie, if the value is True), another branch of the algorithm is executed by the False condition.

Conditions can be quite complex, they can be combined, and the following operators are applied: AND, OR, and NOT. So, for the AND operator:

Truth And Truth \u003d Truth;
True And False \u003d False;
Lies And Truth \u003d Lies;
Lies AND Lies \u003d Lies.

For the OR operator, it is enough that one of the operands is True, then the value of the combination will be True. The value is False only if both operands are False.

The operator does NOT simply invert the current value (False to True, True to False).

Using a combination of these operators, you can build quite complex conditions. When constructing complex conditional statements, consider the precedence.

The NOT operator has the highest priority, followed by the AND operator, followed by the OR operator. What is in parentheses has the highest priority and gets executed first.

For example, let's prioritize (sequence of execution) for the operations in the above expression:

NOT (Condition1 OR Condition2) AND Condition3 OR Condition4
1. Result1 \u003d (Condition1 OR Condition2);
2. Result2 \u003d NOT Result1;
3. Result3 \u003d Result2 AND Condition1;
4. Result \u003d Result3 OR Condition4;

There is a conversion rule:

NOT (Condition1 OR Condition2) \u003d NOT Condition1 AND NOT Condition2.

However, one should not always strive to simplify the expression, because often, logically, the expanded expression is easier to read.

Assignment operator

The assignment operator should not be confused with equality, even though they have the same spelling.

The principle of the assignment operator is such that the left value (the variable on the left side) is assigned the value that is to the right of the equal sign. Let's look at an example:

Variable1 \u003d Variable2 \u003d Variable3;

Variable1 is assigned an equality value from Boolean logic, i.e. True if Variable2 \u003d Variable3, or False otherwise.

When testing for the position of a novice programmer, the task is often used: to swap the values \u200b\u200bof two variables in places.

This problem is solved using the assignment operator and has two solutions.

Solution # 1 using a temporary variable:
TemporaryVariable \u003d Variable1;
Variable1 \u003d Variable2;
Variable2 \u003d TemporaryVariable;

Solution # 2:
Variable1 \u003d Variable1 + Variable2;
Variable2 \u003d Variable1 - Variable2;
Variable1 \u003d Variable1 - Variable2;

Conditional operator

There is such operator If, after which it is necessary to describe some condition (the condition itself can be quite large). The condition is followed by the word Then and the executed operators.

This may be followed by the keyword Else and a series of other operators. If there are several different conditions, you can use a series of keywords Otherwise, if (see example below). The whole construction must end with a keyword End Iffollowed by a semicolon.

In addition to simple and multiple conditions, there is an abbreviated form of the conditional operator:? (Condition, Expression1, Expression2);

If the condition is true, then Expression1, otherwise - Expression2... Example code: DearProduct \u003d? (Product.Price\u003e 100000, True, False);

In practice, instead of comparison records with value True (Lying) type:

If Variable \u003d True Then
and
If Variable \u003d False Then

the equivalent records are actually used:

If Variable Then
and
If NOT Variable Then

Cyclic Operators

For any type of cycle, it is necessary to explicitly indicate the end of this cycle using the keyword End of cycle... There are several types of loops.

Loop over counter - a cycle with a fixed number of repetitions. The condition for exiting the cycle is exceeding the limit value. An example of use for calculating the value of A!

A \u003d 5;
Factorial \u003d 1;
For Counter \u003d 1 By A Cycle
Factorial \u003d Factorial * Counter;
End of Cycle;

Conditional loop - is executed while the condition of this cycle is true. Example:

Remaining Amount \u003d 1000;
Additional Item Price \u003d 243;
Quantity \u003d 0;
While RemainderSum\u003e 0 Cycle
Quantity \u003d Quantity + 1;
Remaining Amount \u003d Remaining Amount - Quantity * Price of Additional Product;
Price of Additional Product \u003d Price of Additional Product * 0.8;
End of cycle
Quantity \u003d Quantity-1;

This cycle calculates how many units of a product can be bought for a given amount (1000 rubles) if after buying each unit of a product its previous price is multiplied by a factor of 0.8. The original price of the product is 243 rubles.

An example of an error when using this type of loop for beginners is an eternal loop, when initially the loop condition is true, but within the loop itself it does not change in any way.

Loop through collections (another name for each).

The Platform has a fairly large number of collections (these are containers that contain elements of a certain type).

You can traverse the elements of a collection using a loop of a special kind.

For example, there is an array of numbers, you need to calculate the sum of all the elements of the array:

Amount \u003d 0;
For Each Item From Array Loop
Sum \u003d Sum + Element;
End of Cycle;

There are special operators for loops: Proceed and Abort.

If at some point in the cycle the execution of further operators of this loop becomes meaningless, then to return to the beginning of the loop and organize its next loop, the operator Proceed.

Operator Abort allows you to terminate the execution of the loop even if the loop condition is true.

This concludes our first acquaintance with development in the internal 1C language.

What about Hello World? We haven't written it yet, have we? Yes, but nothing prevents you from doing it yourself. knowledge is ALREADY enough. Well, if it doesn't work out, then you can peek here.