1

Is there a way to refer to a dinamicaly created object in "with" segment to let's say pass this object somewhere else?

I have a simple code like this

var
  someObject: TSomeObject;
begin
  someObject := TSomeObject.Create;
  try
    someObject.someProperty := 1;

    SomeOtherProcedure(someObject);
  finally
    someObject.Free;
  end;
end;

there is a variable that is being passed to SomeOtherProcedure. Now I am trying to drop someObject variable and use "with" segment to have something like this

begin
  with TSomeObject.Create do
    try
      someProperty := 1

      SomeOtherProcedure( < what goes here ?? > );
    finally
      Free;
    end;
end;

I do not want to have something like

var
  someObject: TSomeObject;
begin
  someObject := TSomeObject.Create;

  with someObject do
  (...)

Is this even possible to refer to an object that is being created in "With"?

Thanks!

Surrogate
  • 331
  • 3
  • 11
  • Not very much. Think of it as one of the reasons to not to use 'with'. – Sertac Akyuz Sep 23 '14 at 11:42
  • Not realy. You can make an ClassHelper for TSomeObject. But not a Generic class helper – Jens Borrisholt Sep 23 '14 at 11:44
  • Or you could add an Instance function to your class Type TSomeObject = class(TObject) public function Instance : TSomeObject; end; { TSomeObject } function TSomeObject.Instance: TSomeObject; begin Result := self; end; – Jens Borrisholt Sep 23 '14 at 11:49
  • Not that I am advocating this, but you _could_ add a `Me` function to your class, which returns `Self` and use that here. – 500 - Internal Server Error Sep 23 '14 at 11:50
  • 1
    @Jens - You're assuming TSomeObject is some custom class. – Sertac Akyuz Sep 23 '14 at 11:57
  • Correct else you have to stick with a class helper. But stiil I think my "solution" are bad code read more here: http://wiert.me/2009/04/27/delphi-bizarre-use-of-class-helpers-to-circumvent-with/ – Jens Borrisholt Sep 23 '14 at 11:59
  • I see solutions that require the writing of a class helper, or adding an instancue function, or adding a `Me` function, etc. just to avoid having to write two lines instead of one? Does that really make sense? Anyway, the best thing is to avoid `with` altogether. – Rudy Velthuis Sep 23 '14 at 16:38

1 Answers1

2

No you can't and use of the With statement (especially in cases like the one you illustrated) should be avoided as it can cause more problems than it solves.

Consider the following code :-

    Procedure TMyForm1.btnProcessClick(Sender : TObject);
    Begin
      With TMyForm2.Create(Nil) Do
      Begin
        Try
          Caption := 'Processing....';
          DoSomeProcessing;
          DoSomeMoreProcessing;
        Finally
          Free;
        End;
      End;
    End;

Supposing TMyForm1 also has a method called DoSomeProcessing? Which one will get called? Which form caption will change to say 'Processing...'? It's not immediately clear the method that will be called. The situation becomes even more complicated when you start referencing properties. Don't expect the Debugger to be able to help you either. Now all of this might not cause you too much of an issue now when the code is still fresh, but what about 6 months or a year later? You have caused yourself a whole load of grief all to save yourself a bit of typing.

Andy_D
  • 2,340
  • 16
  • 21