Re: Access instance created in with .. do

Giganews Newsgroups
Subject:Re: Access instance created in with .. do
Posted by: Christian Gorski (no.spam@no.spam.com)
Date:Wed, 21 Jun 2006

> First of all "with" is evil ! :-))

Why?

> Second, if you really insist on using it, then you need to add a method,
> that returns self, to the class that you are trying to use
>
> TObj = class
> ...
> public
>  function GetThis: TObj;
>  ...
> end;
>
> implementation
>
> function TObj.GetThis: TObj
> begin
>  return self;
> end;

That´s not an option for me.

> Thirdly, if you are going to call free in the with block I suggest you also
> include a correctly declared try..finally block inside it.
>
> begin
>  with TObj.Create do
>  try
>    SomeFunc(?);
>  finally
>    Free;
>  end;
> end;

It was only a simple example not taken from real life to show what I am
talking about. ;-)

> But better still would be to do things properly and not have to mess up your
> classes for the sake of a couple of extra keypresses when typing the code.
>
> var
>  obj: TObj;
> begin
>  obj := TObj.Create;
>  try
>    SomeFunc(obj);
>  finally
>    obj.Free;
>  end;
> end;

This is especially what I didn´t want to type, but obviously I have to.

Replies

In response to

Re: Access instance created in with .. do posted by Joanna Carter [TeamB] on Wed, 21 Jun 2006