1

In a with block I need to pass the object which is used with the with statement as function parameter.

with TMyObject.Create do
begin
  //...
  MyFunction( (* here I have to pass the created object *) );
  //...
end;

The same goal could obviously be achieved as follows:

var
  MyObj : TMyObject;
begin
  MyObj := TMyObject.Create;
  //...
  with(MyObj) do
  begin
     //...
     MyFunction(MyObj);
  end;
  //...
end;

...but I would know if there's a way to simplify my code and get the reference, without declaring the MyObj variable.

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
  • AFAIK there is no way to refer to the "current with object". – ain Feb 08 '17 at 08:40
  • I think you should not use "with" here at all and it will be best solution. Some linke here: http://stackoverflow.com/questions/71419/why-should-i-not-use-with-in-delphi http://stackoverflow.com/questions/514482/is-delphi-with-keyword-a-bad-practice – Andrei Galatyn Feb 08 '17 at 08:40
  • 2
    It's a sign from God ....... – David Heffernan Feb 08 '17 at 08:43
  • @AndreiGalatyn: I fully agree with you and I never use `with` in my projects, unfortunately in this case I have to tell to another programmer how to change its code and I wanted to avoid to make him change hundreds of lines in which he's using the `with` statement. Anyhow, it seems it's the only way... Probably its better this way! – Fabrizio Feb 08 '17 at 08:56
  • 1
    I have never understood the obsession to use `with...` that some people exhibit. But to still use it when you anyway have to declare a local variable for other reasons ... wow! – Tom Brunberg Feb 08 '17 at 08:59
  • @TomBrunberg: Please read my previous comment – Fabrizio Feb 08 '17 at 09:20
  • Yes, I read it when it popped up right after I posted my (interrupted by a phone call) comment. Your comment explains but doesn't justify leaving the hundreds of lines unchanged. Of course, this is only my opinion, and I'm not to say what your fellow programmer is to do. – Tom Brunberg Feb 08 '17 at 09:36

0 Answers0