简体   繁体   中英

Removing part of condition in some cases

Is there a way to remove part of condition in IF statement depending if I need it or not. Next code is example, because in my code there are a lot of user defined functions and procedures in my language:

IF A THEN
Q := TQuery.Create(Application);
IF B AND C AND D AND E AND Q.FieldByName('Example').AsInteger = 1 then
BEGIN
...
END

So, let's say after creating TQuery that I have imported some data into it (I didn't write that part of code here). Is there a way to remove part with Q in the second IF statement if the Q wasn't created (because the condition A was not satisfied) or do I have to writhe the whole IF statement again just without Q part?

I was thinking using something like CASE to check if Q is created and if it is not, just to skip that part of sentence. If it is possible, how can I skip it?

Quick and dirty way

IF B AND C AND D AND E AND (NOT A OR Q.FieldByName('Example').AsInteger = 1) then

As a note, try to keep your if conditions simpler.

EvaluationRequested := A AND C ...
QueryNeedsAval := NOT A OR ... 
if EvaluationRequested AND QueryNeedsAval then 
begin
   ...
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM