[Lazarus] Break in 'case...of' block.

Dave Coventry dgcoventry at gmail.com
Sun Aug 22 13:55:05 CEST 2010


Great answers!

Thanks, guys! :)

On 22 August 2010 11:36, Michael Van Canneyt <michael at freepascal.org> wrote:
>
>
> On Sun, 22 Aug 2010, Dave Coventry wrote:
>
>> I have a function that tests for a series of different conditions
>> according to a value in a 'case...of' block.
>>
>> Result:=false;
>> mylist:=TStringlist.Create;
>> case i of
>>  1:
>>  begin
>>     if condition1 then
>>     begin
>>        Result:=true;
>>        break;
>>     end;
>>     if condition2 then
>>     begin
>>        Result:=true;
>>        break;
>>     end;
>>     if condition3 then
>>     begin
>>        Result:=true;
>>     end;
>>  end;
>>  2:....;
>>  3:....;
>> end;
>> mylist.free;
>>
>> The above does not work as the 'case...of' is not considered to be a loop.
>> Error: BREAK not allowed.
>>
>> I could just exit the function, using 'exit' instead of 'break' but I
>> think I need to free the mylist TStrings.
>>
>> Is there a way of doing this or is my best way just to replace 'break'
>> with 'mylist.free' and 'exit'?
>
> If you use a try/finally block (and you always should when allocating
> resources) then an exit will go through the finally block. So you can simply
> use exit.
>
> mylist:=TStringlist.Create;
> try
>  Case i of
>    // Use exit
>  end;
> finally
>  MyList.Free;
> end;
>
> --
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
>




More information about the Lazarus mailing list