Ver Mensaje Individual
  #25  
Antiguo 20-07-2015
Avatar de olbeup
olbeup olbeup is offline
Miembro
 
Registrado: jul 2005
Ubicación: Santiago de la Ribera (España)
Posts: 688
Reputación: 21
olbeup Va camino a la fama
Cita:
Empezado por Lepe Ver Mensaje
Coincido en que hay que prohibirlo.

http://stackoverflow.com/questions/7...with-in-delphi

Si se usa como :
Código Delphi [-]
with Tquery.create(nil) do begin 
  open
  sql := 'blah blha';
  ExecSql;
  Free;
end;
Pues vale... aunque eso debería ser una función llamada ExecSql( const sql : string) con todo el código dentro, así que aún así no debería usarse el with.

El problema que he visto en varios sitios es este:
Código Delphi [-]
procedure TForm3.HazAlgo();
with Form1, Form2 do begin
  ... un chorro código aquí
  ClientWitdth := 300;
  ... un chorro código aquí
end;
end;
A qué se refiere el Clientwidth, a Self, a Form1, a Form2... y las rutinas que se llaman dentro del with, ¿donde están definidas?.

Pues eso, claridad, legibilidad, seguridad... Mejor "sin" que "con" , y si conduces, mejor "sin" .
El último objecto (Form2) es el que prevalece y el ClienteWidth sólo afectara el cambio al Form2.
Código Delphi [-]
with Form1, Form2 do begin
es igual a
Código Delphi [-]
with Form1 do
  with Form2 do begin
    ClientWitdth := 300;
    ...
    ...
  end;
Si los objetos obj1, obj2, objn son iguales, sólo afectara al último objecto, si tienes todo esto claro, pues NO tendrás problema con el with.

ejemplo:
Código Delphi [-]
type TDate = record
  Day: Integer;
  Month: Integer;
  Year: Integer;
end;
var OrderDate: TDate;
La siguiente con la declaración.
Código Delphi [-]
with OrderDate do
  if Month = 12 then
  begin
    Month := 1;
    Year := Year + 1;
  end
  else
    Month := Month + 1;
Esto es equivalente a
Código Delphi [-]
if OrderDate.Month = 12 then
begin
  OrderDate.Month := 1;
  OrderDate.Year := OrderDate.Year + 1;
end
else
  OrderDate.Month := OrderDate.Month + 1;
Y si quieres coger el año del Form2
Código Delphi [-]
with OrderDate do
  begin
    Year := Form2.Year;
    ...
  end;
Un saludo.
__________________
Al hacer una consulta SQL, haz que los demás te entiendan y disfruten de ella, será tú reflejo de tú saber.
Responder Con Cita