Why not with ... do the expression and the rectangle work flawlessly together?

Recently, I noticed that at any time I use the Rectangle variable with the C ... do instruction, for some reason it does not work at all.

For instance:

var bounds:=new Rectangle(0,0,0,0); with bounds do begin X:=1; Y:=2; Width:=33; Height:=44; end; 
Values

bounds' remain zeros, not in the with statement. However, if I do the following, it works fine.

 var bounds:=new Rectangle(0,0,0,0); bounds.X:=1; bounds.Y:=2; bounds.Width:=33; bounds.Height:=44; 

Is there a reason why this will be done.

+4
source share
1 answer

What Hans Passant is trying to understand is that the c operator creates a copy of the boundaries, works on it, and then discards it. I do not have enough information to verify this, but I feel that this is unlikely. The Delphi assignment operator works by reference, so implicit small copies do not actually happen that often.

However, the β€œc” statements deliberately create a special kind of variation of hell. You could grab the field inside the borders, or you could grab the field from the containing method, or you could even grab the field from the previous closed c statement. Automatic refactoring cannot concern the with statement. Adding a field to the class the operator is working with can split the method .

Consider

 with myLongNamedComponent.anotherLongNamedChild.pedanticRectangle do begin x:=1; y:=2; width:=33; height:=44; end; 

Actually, it’s better written as

 var bounds := new Rectangle(0,0,0,0); bounds.x := 1; bounds.y := 2; bounds.width := 33; bounds.height := 44; myLongNamedComponent.anotherLongNamedChild.pedanticRectangle := bounds; 

TL: DR; the c operator is no longer considered good coding practice.

+1
source

Source: https://habr.com/ru/post/1383953/


All Articles