Clear sky!

The situation is this: There is an array. I pass it to the procedure by value. I work inside the procedure allegedly with a copy of the array, but in the end it turns out that the link to it is still transmitted, and the value of the transmitted array changes.

part of the code in the procedure:

Процедура ЗаполнитьДаты(Периоды, Знач МойМассив, КолонкаНачала, КолонкаКонца) ... Пока Дата(МойМассив[0] + " 00:00:00") < ПерваяДатаЛиста Цикл МойМассив.Удалить(0); Если МойМассив.Количество() = 0 Тогда прервать; КонецЕсли; КонецЦикла; ... КонецПроцедуры; 

What am I doing wrong, and how to do it right?


It is interesting that even if I do this:

 Массив1 = Массив2; Массив1.Удалить(0); 

Array2 will change. This begs the question - how in the usual assignment to copy the value, and not the link?

    1 answer 1

    Passing a parameter by value does not copy the object. The object remains the same. By calling methods in a procedure, the call goes to the methods of the original object. Now, if the procedure would be МойМассив = Новый Массив() , then there would be no change to the original object.

    Pascal analogue:

     procedure ClearStringListValue(SL: TStringList);//По значению begin SL.Clear; end; procedure ClearStringListLink(var SL: TStringList);//По ссылке begin SL.Clear; end; 

    and there and there SL is cleared. Pass by value will be visible here:

     procedure ClearStringListValue(SL: TStringList);//По значению begin SL:=TStringList.Create; SL.Add('by value'); end;//Значение не изменится procedure ClearStringListLink(var SL: TStringList);//По ссылке begin SL:=TStringList.Create; SL.Add('by link'); end;//Будет новый стринглист 

    Accordingly, in order to avoid changes, you need to manually copy the object. For an array there is no copying method, well, it means on its own in a loop ...

    Threat The transfer of parameters by reference and by value when calling procedures and functions

    • These are innovations 1С? In other cases, passing a parameter by value creates a copy of the object (if necessary) so that the original object does not change. - alexlz
    • @alexlz I gave an example from pascal / delfe. There is no copy constructor as in pluses, passing a parameter by value copies the link, not the object. In javascript, too, because objects are passed by reference, only primitive types by value. So, if you need a copy of the object, copy it in the functions yourself. Such is ideology, and it was not invented in 1s, at least there is nothing supernatural in it, in 1s there are enough other quirks :) - Yura Ivanov
    • If I'm not mistaken - 1C is a licensed VBA, which was patriotic translated into Russian - renegator
    • So in delphi, when passing parameters by value, a copy is not created? How interesting. "And the men do not know" (from the advertisement). What then is SL in your example? And you can change its attributes, and in the calling program, these changes will be visible? Wow how. Poor N. Wirth, which they created in his pascal. By the way, from the docks on your link: “Inside the called Procedure Edit Table (), a new copy of the value table is created, to which three rows are added. This does not affect the state of the Ts value table that is passed as an actual parameter when you call the Edit Table () procedure” - alexlz
    • @alexlz, I see no reason for your sarcasm. when passing a parameter by value, a copy of the link will be created. "Copy link" from "copy object" is easy to distinguish. - Yura Ivanov